IT SOLUTIONS
 Coder
 Coder Group Home eMag: Coder Edition WEEKLYLESSON MYGROUPSETTINGS
 Message Board
 Coder KB
 Coder Study Test PRESTWOODCERTIFIED
 Coder Store PRESTWOODSTORE Store Home Buy Coder Items
 Members Only

#### Prestwood eMagazine

July Edition
Subscribe now! It's Free!

 ► KB ► Role-Based T... ► Coding & OO ► General Codi...
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.

 To/From Code Guide: Round Floating Point Numbers (... Language Examples  A flashcard from our Coder Flashcards Library  A tip from our Coder Tips Page

### 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.

 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 (=, <>)

Share a thought or comment...