# floating point anomaly?

Rajesh Chandra

Ranch Hand

Posts: 55

Ulf Dittmer

Rancher

Posts: 42967

73

posted 10 years ago

Go back to the classics: What every computer scientist should know about floating-point arithmetic

Jeff Albertson

Ranch Hand

Posts: 1780

Rajesh Chandra

Ranch Hand

Posts: 55

posted 10 years ago

In floating point arithmetic, most decimals are rounded - simply because it is impossible to define it in binary. Where arithmetic has problems with 1/3, floating point arithmetic has problems with many other numbers as well, like 1/10. In binary, this leads to a repetition similar to 1/3 or 1/9.

Therefore, in any programming language, floating point comparison should be one by checking if the difference is small enough:

Where EPSILON is a very small value - the smaller EPSILON is, the smaller the fault in comparison.

Ideally EPSILON would of course be 0.0, meaning f1 == f2, but as you've just read - you can't rely on that comparison.

Therefore, in any programming language, floating point comparison should be one by checking if the difference is small enough:

Where EPSILON is a very small value - the smaller EPSILON is, the smaller the fault in comparison.

Ideally EPSILON would of course be 0.0, meaning f1 == f2, but as you've just read - you can't rely on that comparison.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Eric McIntyre

Greenhorn

Posts: 26

Ulf Dittmer

Rancher

Posts: 42967

73

Eric McIntyre

Greenhorn

Posts: 26

Ashu Sindhu

Greenhorn

Posts: 5

posted 10 years ago

Try the following links:-

http://www.codeproject.com/dotnet/ExtremeFloatingPoint1.asp

http://www.cs.berkeley.edu/~wkahan/ieee754status/why-ieee.pdf

This should help.

Thanks,

Ashu Sindhu

http://www.codeproject.com/dotnet/ExtremeFloatingPoint1.asp

http://www.cs.berkeley.edu/~wkahan/ieee754status/why-ieee.pdf

This should help.

Thanks,

Ashu Sindhu