From the January 2016 Issue of Prestwood eMag
Coder General Coding Concepts:
Round Floating Point Numbers
Posted
15 years ago on 2/13/2009
Take Away: When comparing floating point numbers, make sure you round to an acceptable level of rounding for the type of application you are using.
KB101911
Floating Point 'Equality' Problem
Because computers have trouble representing floating point values, you may want to reserve the use of floating point literals for imprecise measurements such length, height, weight, etc. The very nature of measurements is imprecise.
For example, in your computer language try the following if statement (psuedo code here so translate it to your language).
//psuedo code if (0.1 + 0.1 + 0.1 = 0.3) then print "correct" else print "not correct"
Out of the languages documented here, only Delphi evaluates this code correctly.
More Info
Code:
Access VBA Comparison Operators (=, <>)
Code:
ASP Classic Comparison Operators (=, <>)
Code:
C# Comparison Operators (==, !=)
Code:
C++ Comparison Operators (==, !=)
Code:
C++/CLI Comparison Operators (==, !=)
Code:
Delphi Comparison Operators (=, <>)
Code:
Delphi Prism Comparison Operators (=, <>)
Code:
Java Comparison Operators (==, !=)
Code:
JavaScript Comparison Operators (==, !=)
Code:
ObjectPAL Comparison Operators (=, <>)
Code:
Perl Comparison Operators (==, !=)
Code:
PHP Comparison Operators (==, != or <>)
Code:
VB Classic Comparison Operators (=, <>)
Code:
VB.Net Comparison Operators (=, <>)
0 Comments.
Share a thought or comment...
Tip Contributed By Mike Prestwood:
Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions . Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books . If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.
KB Article #101911 Counter
23033
Since 2/13/2009