# Issues with Float Data Thpes [Answered]RSS

## 10 replies

Last post Aug 10, 2017 09:39 AM by Breaker2112

• Breaker2112

Member

2 Points

6 Posts

### Issues with Float Data Thpes

Aug 04, 2017 09:22 PM|Breaker2112|LINK

Hi. I need some help on Float Data types. Can someone articulate to me (without quoting the typical online definition ) what a Float Data type loses in its precision? Specifically, does it start to round the value after a certain length digit?
For example:

If the actual data value was 12,389,999.83 would the float data type round it to 12,390,000?
• mgebhard

All-Star

40761 Points

17064 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 12:47 PM|mgebhard|LINK

It has to do with binary math, hardware, and speed.  There is no way to represent 1/3 in binary as the decimal repeats indefinitely.  The calculation 1/3 * 2 must be limited otherwise the computer will churn for eternity. That's where standards come in.  Every computer system should not arbitrarily pick how to handle 1/3 as that would cause compatibility issues between systems.  We have standards like IEEE 1987 which recommends how the systems should handle 1/3.

Keep in mind, the science behind floating point calculations is far more complicated than handling 1/3.  There are other technical challenges with floating point math and you can certainly learn the subject.  This will get you started.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

• Breaker2112

Member

2 Points

6 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 12:54 PM|Breaker2112|LINK

Sorry to ask, but can you provide the risks of using flat as it relates to financial data? In short, my worry is that financial numbers within a float datatype will not reconciled to true balances. Is that true? If so, after how many digits does it get impacted? Is there any concern with my example above?
• mgebhard

All-Star

40761 Points

17064 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 12:59 PM|mgebhard|LINK

#### Breaker2112

Sorry to ask, but can you provide the risks of using flat as it relates to financial data? In short, my worry is that financial numbers within a float datatype will not reconciled to true balances. Is that true? If so, after how many digits does it get impacted? Is there any concern with my example above?

Use the decimal type for financial calculations.

• Breaker2112

Member

2 Points

6 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 01:05 PM|Breaker2112|LINK

I would if I could, but this is a "black box" data system. I need to know what the risk is to using the float datatype with financial numbers. At what point does it impact The true value. I have no option to change it to decimal point when the data is captured
• mgebhard

All-Star

40761 Points

17064 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 01:25 PM|mgebhard|LINK

#### Breaker2112

I would if I could, but this is a "black box" data system. I need to know what the risk is to using the float datatype with financial numbers. At what point does it impact The true value. I have no option to change it to decimal point when the data is captured

As far as I know it is not a straight forward answer as it depends on how you are currently handling precision and types of number used in the calculations.

It's not about decimal point precision.  It's how base 10 numbers are represented in binary as not all base 10 number can be represented in binary.

• Breaker2112

Member

2 Points

6 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 01:50 PM|Breaker2112|LINK

Thanks. That helps a little bit, but I'm still not there incomplete understanding. It helps to know that is not a "decibel thing". Do you think you can come up with some examples of where the precision of the conversion between float and a small Would result in an issue where financials might not balance?
• mgebhard

All-Star

40761 Points

17064 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 04:06 PM|mgebhard|LINK

#### Breaker2112

Thanks. That helps a little bit, but I'm still not there incomplete understanding. It helps to know that is not a "decibel thing". Do you think you can come up with some examples of where the precision of the conversion between float and a small Would result in an issue where financials might not balance?

The margin of error is based on the number and the arithmetic.

```    class Program
{
static void Main(string[] args)
{

float fvalue = 123456789.12F;
float frate = -0.0123f;

decimal dvalue = 123456789.1m;
decimal drate = -0.0123m;

Console.WriteLine("Float\t\tDecimal");
for (int i = 0; i < 15; i++)
{
fvalue = fvalue + (fvalue * frate);
dvalue = dvalue + (dvalue * drate);
Console.WriteLine("{0:N10}\t{1:N10}", fvalue, dvalue);
}

Console.WriteLine();

}

}
```

```Float           Decimal
121,938,300.0000000000  121,938,270.5940700000
120,438,400.0000000000  120,438,429.8657629390
118,957,000.0000000000  118,957,037.1784140549
117,493,900.0000000000  117,493,865.6211195620
116,048,700.0000000000  116,048,691.0739797914
114,621,300.0000000000  114,621,292.1737698399
113,211,500.0000000000  113,211,450.2800324709
111,819,000.0000000000  111,818,949.4415880715
110,443,600.0000000000  110,443,576.3634565382
109,085,100.0000000000  109,085,120.3741860228
107,743,400.0000000000  107,743,373.3935835347
106,418,100.0000000000  106,418,129.9008424572
105,109,200.0000000000  105,109,186.9030620950
103,816,400.0000000000  103,816,343.9041544313
102,539,400.0000000000  102,539,402.8741333318

```

```        static void Main(string[] args)
{

float fvalue = 123456.12F;
float frate = -0.0123f;

decimal dvalue = 123456.12m;
decimal drate = -0.0123m;

Console.WriteLine("Float\t\tDecimal");
for (int i = 0; i < 15; i++)
{
fvalue = fvalue + (fvalue * frate);
dvalue = dvalue + (dvalue * drate);
Console.WriteLine("{0:N10}\t{1:N10}", fvalue, dvalue);
}

Console.WriteLine();

}```
```Float           Decimal
121,937.6000000000      121,937.6097240000
120,437.8000000000      120,437.7771243948
118,956.4000000000      118,956.3924657647
117,493.2000000000      117,493.2288384358
116,048.1000000000      116,048.0621237231
114,620.7000000000      114,620.6709596013
113,210.8000000000      113,210.8367067982
111,818.3000000000      111,818.3434153046
110,443.0000000000      110,442.9777912963
109,084.5000000000      109,084.5291644634
107,742.8000000000      107,742.7894557405
106,417.6000000000      106,417.5531454349
105,108.6000000000      105,108.6172417460
103,815.8000000000      103,815.7812496725
102,538.8000000000      102,538.8471403016

```

• Breaker2112

Member

2 Points

6 Posts

### Re: Issues with Float Data Thpes

Aug 05, 2017 11:39 PM|Breaker2112|LINK

Awesome. Thanks very much. I think your numeric examples was exactly what I was after! I greatly appreciate the effort!!
• limno

All-Star

122864 Points

9901 Posts

Moderator

### Re: Issues with Float Data Thpes

Aug 07, 2017 01:18 PM|limno|LINK

Float is not a data type you can use if you want a precise balance. It is very rare in an application you can use float in most applications. Float is an approximate data type. You can should use numerical/decimal to get correct numeric number.

Jingyang Li
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
• Breaker2112

Member

2 Points

6 Posts

### Re: Issues with Float Data Thpes

Aug 10, 2017 09:39 AM|Breaker2112|LINK

Yes, that's what this entire discussion is about. We know what it is and why. We know we 'can should' use decimal. However as explained above, this is a black-box cots application. The issue is, we cannot recast the data type, and we are looking for impacts. The question has been answered.