• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is 2.0%0.4=3.9999?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tested the modulo operator at Netbeans 4.0 and I got 2.0%0.4=3.9999
Isn't the result 0? I am not sure. Would you please give me some pointers?
Thank you in advance.

Below is the output
-------------------------------------------
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\kevin\IncrementOperator\build\classes
compile:
run:
5.5/1.5=3.6666666666666665
5.5%1.5=1.0
2.0/0.4=5.0
2.0%0.4=0.3999999999999999
BUILD SUCCESSFUL (total time: 1 second)
--------------------------------------------

Kai Wen
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's not 3.999999999999999; it's 0.3999999999999999, which is as near to 0.4 as precision will allow. The problem is that 0.3999999999999999 does not allow the division to happen "one more time" and produce 0.0 as the remainder. If it could have calculated exactly then it would have got 0.0. Expect these weird results with floating point arithmetic.

I would expect that SCJP will be limited to integer arithmetic for this operation.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, if your real name is Kai Wen why do you not use that as your displayed name instead of "Kevin Ham"? That would be better conformance with our JavaRanch Naming Policy

Thanks
-Barry
 
kai wen ham
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

Thank you for your help. I have also changed my name to Kai Wen Ham.

Have a nice day!

Kai Wen
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following Sun article, scroll down about half way to the section, "Some Things You Should Know About-Floating Point Arithmetic."

http://java.sun.com/developer/JDCTechTips/2003/tt0204.html

(Also read the API for the BigDecimal constructor that takes a double as an argument.)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scary isn't it?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Scary isn't it?




Personally, I would be uncomfortable writing a program that required "precise" floating-point calculations (even using BigDecimal). I would need to spend some time researching and testing what I "think" I know about this.
[ December 13, 2004: Message edited by: marc weber ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the first subjects I studied long long ago in computing topics was Numerical Analysis. It was certainly part of my maths degree and was part of the computer science degree course. It's scary seeing how many programmers test for exact equality when it comes to floating point arithmetic, rather than equality within a given tolerance.

In the example above for the % operator one has to realize that any remainder between 0 and 0.39999999999999999 (up to somewhere) is a valid answer for a mod 0.4 calculation. And 0.39999999999999999999 is close to 0 when it comes to mod 0.4 arithmetic - its near -0.0000000000000000001 (exact number of zeros not important).

Hell! I'm so rusty when it comes to this math stuff nowadays.
[ December 13, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem is that so many people are never taught the first thing about this and most tutorials and books don't mention it (if they even use floating point math at all and don't stick to integer math which looks nicer).

This is possibly a leftover from the ancient (early 1990s) period when many computers couldn't do floating point math at all and if they could it was several orders of magnitude slower than integer math.
I've a book from that time that goes into the subject of writing your own floating point mathematics unit using only integer calculations, people went that far to avoid using floating point math (in this case it was for performance reasons, in other cases it would have been precision).
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
Problem is that so many people are never taught the first thing about this...


Absolutely! This should be fundamental.

A gap between computer programming and computer science...(?)
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, just teaching curiculums stuck in the 1970s (as is evident seeing the number of questions on C(++) boards about ancient DOS compilers.
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic