• Post Reply Bookmark Topic Watch Topic
  • New Topic

double data type behaviour  RSS feed

 
amitCh Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use double data type in a for loop for precise operations and just to see if there could be any problem doing that I tested a small code :

The output I was expecting is :
-0.1
-0.09
-0.08
-0.07
-0.06
-0.05
-0.04
-0.03
-0.02
-0.01
0.00

But the output of the code is :
-0.1
-0.09000000000000001
-0.08000000000000002
-0.07000000000000002
-0.06000000000000002
-0.05000000000000002
-0.040000000000000015
-0.030000000000000013
-0.02000000000000001
-0.01000000000000001
-1.0408340855860843E-17

Why is the code not working the way I expected, I think it has something to do with any property of double but I am not sure.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitCh Singh wrote:
Why is the code not working the way I expected, I think it has something to do with any property of double but I am not sure.


You are correct. This has less to do with Java in general, and more to do with the floating point format that Java uses to represents double types -- the IEEE 754 floating point standard.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with how numbers are represented in java, which is based off an IEEE standard.

The short version - you can't use floats or doubles for super-precised computations of any kind, because they simply are NOT super-precise. They are good enough for most things.

You search around these forums for many threads on this very subject - most of which would have better answers than I could give.
 
amitCh Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Henry Wong and @fred Rosenberger, I searched the forum and learned that the most precise data type offered by java is BigDecimal, but it is very slow.
I am trying to get some points on an ellipse using its equation, and the only way I know to do that is by using '==' operator which won't work here, can I somehow round off the numbers to required significant figures, if not, what other options do I have?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitCh Singh wrote:
I am trying to get some points on an ellipse using its equation, and the only way I know to do that is by using '==' operator which won't work here, can I somehow round off the numbers to required significant figures, if not, what other options do I have?


Alternatively, instead of this...



how about this? ...



Henry
 
amitCh Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Alternatively, instead of this...



how about this? ...



Henry

it is working
thanks @Henry Wong
so it means java itself doesn't provide any data type for super precise operations, except BigDecimal, at the cost of speed?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitCh Singh wrote:
it is working
thanks @Henry Wong
so it means java itself doesn't provide any data type for super precise operations, except BigDecimal, at the cost of speed?


Question. How much math are we talking about here? Is it compute intensive? Yes, there is a latency cost of using BigDecimal, but to notice it, you have to do a lot of computations, and it also may have to be the slowest operation.

I would recommend testing the BigDecimal class before rejecting it.

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitCh Singh wrote:thanks @Henry Wong
so it means java itself doesn't provide any data type for super precise operations, except BigDecimal, at the cost of speed?


I'd see it more as: Java provides a data type for super precise operations, but the nature of that requirement is that such a data type will always be slower than primitive types. If they could easily write a class that does what BigDecimal can do, but faster, they'd have replaced BigDecimal with it.

But as Henry says, don't write it off unless you're sure that performance is a problem.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitCh Singh wrote:so it means java itself doesn't provide any data type for super precise operations, except BigDecimal, at the cost of speed?

I can't be sure, but I think this will be true for ALL languages, not just java.

There are always trade-offs between speed, memory, complexity...and in this case, precision.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!