Skip to main content

1.44444-1=0.44443999999999995 ?

9 replies [Last post]
hardwing
Offline
Joined: 2007-04-14
Points: 0

Hello,

I got the following calculation:

public static void main(String[] args) {

double x=1.44444;

x-=1;
System.out.print(x);
}

My expectation is: 0.44444.

but, the final output is: 0.44443999999999995

Can anyone help me get back 0.44444?

Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
linuxhippy
Offline
Joined: 2004-01-07
Points: 0

> but even 1.2-1, I cannot get 0.2. I don't think I
> will get this result in C++.

http://en.wikipedia.org/wiki/Floating_Point

jmguillemette
Offline
Joined: 2003-06-11
Points: 0

Double is considered approprieate for scientific calculations where estimates and speed are more important vs accuracy.

Use a float and you should get the result your looking for.

jwenting
Offline
Joined: 2003-12-02
Points: 0

> Double is considered approprieate for scientific
> calculations where estimates and speed are more
> important vs accuracy.
>
> Use a float and you should get the result your
> looking for.

Wrong. Utterly and completely wrong.
Both 'suffer' from the same thing, which is inherent to floating point mathematics in a discrete system like a binary computer.

jwenting
Offline
Joined: 2003-12-02
Points: 0

> but even 1.2-1, I cannot get 0.2. I don't think I
> will get this result in C++.
>
You would, unless the C++ compiler you use cheats and rounds the result.

> but if i do (1.44444*100000-100000)/100000, I can get
> 0.44444 although it looks stupid but effective....

It is stupid and wrong.

hardwing
Offline
Joined: 2007-04-14
Points: 0

> > but even 1.2-1, I cannot get 0.2. I don't think I
> > will get this result in C++.
> >
> You would, unless the C++ compiler you use cheats and
> rounds the result.

Yes, it rounds the result. Did you try it?

>
> > but if i do (1.44444*100000-100000)/100000, I can
> get
> > 0.44444 although it looks stupid but effective....
>
> It is stupid and wrong.

Did you try to make the program? And what output you got?

klopperq
Offline
Joined: 2005-09-23
Points: 0

The link below takes you to a discussion by Josh Bloch and Neal Gafter that can hopefully explain some of this

http://www.phptr.com/articles/article.asp?p=26887&rl=1

Message was edited by: klopperq

linuxhippy
Offline
Joined: 2004-01-07
Points: 0

This calculation is ok and has something to do with how floating point works.
The number 0.44444 can't be represented by a double therefor its rounded to the next value.

This has nothing to do with java imho, its the same with C++ or .NET for example.

lg Clemens

hardwing
Offline
Joined: 2007-04-14
Points: 0

but even 1.2-1, I cannot get 0.2. I don't think I will get this result in C++.

but if i do (1.44444*100000-100000)/100000, I can get 0.44444 although it looks stupid but effective....

hardwing
Offline
Joined: 2007-04-14
Points: 0

I got the method to overcome this problem from another forum. Using "BigDecimal" class to do the critical calculation.