This week's giveaway is in the OCPJP forum.
We're giving away four copies of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) and have Paul Anilprem on-line!
See this thread for details.
Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Horrendous bug in java.lang.BigDecimal

 
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am developing a math class and have found a truly awful error in java.lang.BigDecimal.

Consider the following code:


The output is:


Which is completely wrong. The correct answer is, of course: 0.64

What do you think about continuing to use BigDecimal in the light of this bug?

--Mario Gianota
 
Sheriff
Posts: 27224
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not a bug. The documentation specifically says that will happen when the result is a non-terminating decimal. Which 40/60 is, since it's 0.666666666666666666666666666....

So I would continue to use BigDecimal, only I would look around for divide methods which allow you to specify the number of decimal places.
 
Paul Clapham
Sheriff
Posts: 27224
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Mario Gianota
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:It's not a bug. The documentation specifically says that will happen when the result is a non-terminating decimal. Which 40/60 is, since it's 0.666666666666666666666666666....

So I would continue to use BigDecimal, only I would look around for divide methods which allow you to specify the number of decimal places.



It is a bug. You are using floating-point to illustrate your answer. Work out the problem on paper and you will see that I am correct.

Thanks for the welcome.
 
Paul Clapham
Sheriff
Posts: 27224
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you clarify that you're claiming that 40/60 is equal to 0.64?
 
Mario Gianota
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Could you clarify that you're claiming that 40/60 is equal to 0.64?



Yeah, no problem:

The answer to 40 / 60
Is the same as the answer to: 400 / 60
Which is 6 remainder 4, or 0.64

Not 0.6666 recurring. This is the problem with modern computers that I have been trying
to point out to people for over 25 years. Division of whole numbers is a *counting* problem.
I know I am correct because I can demonstrate it by counting out the problem.

--Mario


 
Paul Clapham
Sheriff
Posts: 27224
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, all right then. You can't use BigDecimal for that kind of math because it isn't designed for that kind of math. And you can't call its behaviour a bug because it works as designed. You'd have to write your own code to work with your kind of math.
 
Saloon Keeper
Posts: 9338
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us your code that gives the result 0.64 so that we might compile it and run it for ourselves?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think I've ever seen that as the way of symbolising a result and remainder.

Indeed, since we're talking 40 / 60, isn't the result:


?

I must be missing something.

ETA: Oh for crying out loud!
Had to stick that in code tags because the bloody forum abbreviation check kicked in!
 
Saloon Keeper
Posts: 13977
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mario Gianota wrote:The answer to 40 / 60
Is the same as the answer to: 400 / 60


If you're going to make statements like this, it's hard to take you seriously.

Regardless, even when considering that floating point calculations are not precise, they're precise enough that if the answer really was 0.64, Java would never display it as 0.66... but rather as something closer to 0.64.

Simply put: your maths is wrong.
 
Sheriff
Posts: 22655
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mathematically speaking, 40/60 is the same as 4/6 is the same as 2/3. In decimal, this is 0.666666666666666666666666666...., as Paul already mentioned.

If you use long division for 400 / 60 you get 6 with remainder 40, not 4. If you use 40/6 you do get 6 with remainder 4. That does not mean you can append that 4 to the result.
 
lowercase baba
Posts: 13053
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mario Gianota wrote:
The answer to 40 / 60... is 6 remainder 4, or 0.64


6 remainder 4 is not the same as 0.64.  
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind that 40/60 is not 6 remainder 4.
 
Master Rancher
Posts: 4209
57
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

40/60 would be the same as 400/60 IF (and only if) we remember to shift the decimal point to the left afterwards, to compensate for adding a zero to the right of 40, which is shifting the decimal to the right.  That is, if you want to add N zeroes to a number before performing a division, which is shifting the decimal point to the right be N places, then you need to shift the result left by N places.  That applies to both the quotient, and to the remainder.

So if you calculate 40/60 using 400/60 (shift right 1), you get 6 rem 4, and when you shift the decimal point left by 1 to compensate for the initial right shift, you get 0.6 + 0.4/6.  Which is weird-looking, but correct.

If you instead begin with a left shift by 2 places, you get 4000/60 = 66 rem 4, then correct it to to 0.66 + 0.04/6.

If you begin with left shift by 3 places, you get 40000/60 = 666 rem 4, then correct it to 0.666 + 0.004/6.

With 4 places, 400000/60 = 6666 rem 4, corrected to 0.6666 + 0.0004/6.

At some point, you may notice a pattern forming...

Mario Gianota wrote:This is the problem with modern computers that I have been trying
to point out to people for over 25 years. Division of whole numbers is a *counting* problem.
I know I am correct because I can demonstrate it by counting out the problem.



Hmmm, so for 25 years, the rest of the world has been wrong, and you've been unable to convince them?  I wonder why that could be...
 
Paul Clapham
Sheriff
Posts: 27224
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Hmmm, so for 25 years, the rest of the world has been wrong, and you've been unable to convince them?  I wonder why that could be...



I was curious about whether this method of integer division actually formed a consistent system of some subset of arithmetic. I suspect not, but given only one example it's hard to tell.
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forth supports the /MOD word (also spelt /mod), but that wouldn't give this result.
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For having a question mentioned in the January 2021 CodeRanch Journal, congratulations: this question earns you a cow
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic