I.T. Discussion Community!
-Collapse +Expand
Coder
Search Coder Group:

Advanced
-Collapse +Expand Coder Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Coder Store
PRESTWOODSTORE

Prestwood eMagazine

November Edition
Subscribe now! It's Free!
Enter your email:

   ► KBRole-Based T...Coding & OOGeneral Codi...   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Coder General Coding Concepts:
Round Floating Point Numbers
 
Posted 10 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.

 A flashcard from our Coder Flashcards Library
 A tip from our Coder Tips Page

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

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1123A1
Enter key:
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.

Visit Profile

 KB Article #101911 Counter
18810
Since 2/13/2009

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech