Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

problems with precission in java

Thread view: 
juavizga - 10 Apr 2004 20:47 GMT
I need work wiht 30 decimals in a double, but when I try to store :

44*279^7+97*279^5+104*279^4+99*279^3+110*279^2+97*279+77

when a^b is Math.pow(a,b). Exemple 2^2 is 2*2=4

well if I execute this in the calc of windows I get 5.790211750705701434e18
                              -----
but when I execute in my program whit java the result that I get is:
5.7902117507057009e18
                            ----
the result is not the same in java the result is rounded and I lose
precission can anybody or java guru help me, I need the wole precission.

THANKS
Andrew Thompson - 10 Apr 2004 20:55 GMT
> I need work wiht 30..

Messages, Newsgroups?

So far you have posted this qn. (at least)
three times, two to this group and one to
c.l.j.progammer.

IT IS TIME TO BE PATIENT AND *WAIT*
FOR AN ANSWER!!

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Kristoffel - 10 Apr 2004 22:28 GMT
> I need work wiht 30 decimals in a double, but when I try to store :
>
[quoted text clipped - 9 lines]
> the result is not the same in java the result is rounded and I lose
> precission can anybody or java guru help me, I need the wole precission.

I have an other java-result :

public class StrictFP {
    public static void main (String[] args) {
        strictfpCalc ();
        normalCalc ();
    }

    private static strictfp void strictfpCalc () {
        double result = 44 * Math.pow (279, 7)
                 + 97 * Math.pow (279, 5)
                 + 104 * Math.pow (279, 4)
                              + 99 * Math.pow (279, 3)
                              + 110 * Math.pow (279, 1)
                              + 97 * 279 + 77;
        System.out.println (result);
    }

    private static void normalCalc () {
        double result = 44 * Math.pow (279, 7)
                 + 97 * Math.pow (279, 5)
                 + 104 * Math.pow (279, 4)
                              + 99 * Math.pow (279, 3)
                              + 110 * Math.pow (279, 1)
                              + 97 * 279 + 77;
        System.out.println (result);
    }
}

And this is my java-result of Linux (Java version 1.4.2)

5.7902117506971689E18
5.7902117506971689E18

K
Kristoffel - 10 Apr 2004 22:30 GMT
> I need work wiht 30 decimals in a double, but when I try to store :
>
[quoted text clipped - 9 lines]
> the result is not the same in java the result is rounded and I lose
> precission can anybody or java guru help me, I need the wole precission.

I have an other java-result :

public class StrictFP {
    public static void main (String[] args) {
        strictfpCalc ();
        normalCalc ();
    }

    private static strictfp void strictfpCalc () {
        double result = 44 * Math.pow (279, 7)
                  + 97 * Math.pow (279, 5)
                  + 104 * Math.pow (279, 4)
                              + 99 * Math.pow (279, 3)
                              + 110 * Math.pow (279, 1)
                              + 97 * 279 + 77;
        System.out.println (result);
    }

    private static void normalCalc () {
        double result = 44 * Math.pow (279, 7)
                  + 97 * Math.pow (279, 5)
                  + 104 * Math.pow (279, 4)
                              + 99 * Math.pow (279, 3)
                              + 110 * Math.pow (279, 1)
                              + 97 * 279 + 77;
        System.out.println (result);
    }
}

And this is my java-result (Java version 1.4.2) on Linux

5.7902117506971689E18
5.7902117506971689E18

K
Yoyoma_2 - 11 Apr 2004 00:09 GMT
>> I need work wiht 30 decimals in a double, but when I try to store :
>>
[quoted text clipped - 10 lines]
>> the result is not the same in java the result is rounded and I lose
>> precission can anybody or java guru help me, I need the wole precission.

Please wait for a reply or no reply before posting again.  I think your
problem might be in machine epsilon.  Floating point calculations aren't
guaranteed to be accurate usually for 32bit machines its 10^-9. Check
that out.  Or check out your comp sci course that delt with it, or
google it.

good luck

> I have an other java-result :
>
[quoted text clipped - 31 lines]
>
> K
Alex Hunsley - 11 Apr 2004 02:35 GMT
> I need work wiht 30 decimals in a double, but when I try to store :
>
[quoted text clipped - 11 lines]
>
> THANKS

Please don't repost the same question multiple times. It will annoy
people and make you *less* likely to get an answer, if anything.
Thomas Schodt - 11 Apr 2004 09:04 GMT
> I need work with 30 decimals in a double

Why?
If you say why you think you need this, someone might be able to come up
with an alternative way of accomplishing what you are trying to achieve.

> ... the windows calculator gives 5.790211750705701434e18
>                                -----
[quoted text clipped - 3 lines]
> the result is not the same, in java the result is rounded and I lose
> precision, can anybody or java guru help me, I need the whole precision.

The windows calculator is obviously not using Double precision floating
point arithmetic.

To get a precision of 30 decimal digits, you'll need a floating point
mantissa of at least 100 bits.

IEEE (source RFC 754) Double precision floating point is 64 bits of
which the mantissa is 52 bits, so it only gives you 15 decimal digits
precision.

A quick search reveals there is a Quadruple precision floating point
format which is 128 bits of which the mantissa is 112 (source RFC 1832).
Bjorn Abelli - 11 Apr 2004 11:40 GMT
"juavizga" ...

> I need work wiht 30 decimals in a double,
> but when I try to store :

[snip]

> the result is not the same in java the result is rounded
> and I lose precission can anybody or java guru help me,
> I need the wole precission.

You haven't told *why* you need that kind of precision, but you could try to
use the classes BigInteger or BigDecimal.

// Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.