Not Yet Answered # float number issue

Eruditio deceptikon 1,760 Banfa 597 deceptikon 1,760 Banfa 597 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

What happens if you try:

```
if(i>=0.3)
printf("hello\n");
else
printf("ishq\n");
```

?

Some simple decimals cannot be perfectly represented using the internal floating point representation.

0

hi, can anyone please explain why i am getting output as "ishq" even if i=0.3 so if condition must be false. pleas help i am not getting it.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

0

Floating point values are approximations, and any manipulation of a floating point number is like to produce an approximate value that may not be what pure mathmatical logic would suggest the answer should be. For example take

```
float i=0.3;
i += 0.000005 * 30000000;
i -= 0.000005 * 30000000;
```

Logic suggests that at the end of these 3 line i still has the value 0.3 since you have taken off exactly what you added on but on my system it has the value 0.300003 because of the inherent inaccuracies introduced in performing floating point calculations.

You should absolute never use == or != with floating points you can assume that they always equate to false and true respectively. If you do use <, <=, > and >= then you need to be aware and accept that there will be cases where the program produces the opposite result to that which pure logic produces.

0

@deceptikon what should i read in this for getting my answer ?

All of it. Just getting the answer to your immediate question won't stop you from making a similar but seemingly unrelated mistake with floating-point in the future. So it behooves you to study up on the representation and behavior of floating-point in general.

1

Because 0.3f has type float and 0.3 has type double so using 0.3 inloves an implicit conversion of your variable i to type double and since, as discussed, floating point representations are approximations and double as a different represenation to float the conversion can change the value slightly making it higher or lower.

You should not use comparisons in floating point anywhere that you require absolute acuracy, it is fine if you are testing some tolerance, you have a complex equation with a check value and the output is right if the check value is <0.1 because if the check value is actually 0.1000001 then the result was borderline anyway and it is probably fine to discard it.

If you want accuracy, or you are on an embedded platform and you don't want to pull the bloat of the floating-point emulation library into your application then you use scaled integers. A typical example is the banking world, they do not hold money as pounds (£) in a float type with pence as a fractional part. Rather they use a scaled integer and hold money as pence (or may be even 1/10th of pence) in an integer value where practical maths operates as you would expect theoryetical maths to work. Of course you have to watch out for the upper bound of an integer but generally today that can be solved by using some sort of big-int class that has no upper bound.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...