[linux-support] C++: Multiplikation von "double"

maeder+lubge@glue.ch maeder+lubge at glue.ch
Tue May 27 21:35:29 CEST 2008


Markus Wernig wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hallo allerseits
> 
> Ich habe ein Verständnisproblem mit einem C++-Snippet:
> 
> [...]
> unsigned int z = 65535*65535;
> 
> double      z1 = (double)z*(double)z;
> 
> long double z2 = (double)z*(double)z;
> [...]
> 
> Man möchte jetzt doch annehmen, dass z1 und z2 gleichen Wert haben,
> oder? Blöderweise ergibt die entsprechende Ausgabe mit cout dann:
> 
> z  = 4294836225
> z1 = 18445618199572250624.00
> z2 = 18445618199572250625.00
> 
> Hat irgendjemand eine Erklärung dafür???
> (System ist 32bit x86)

Und g++?

man g++:

        The following options control compiler behavior regarding floating
        point arithmetic.  These options trade off between speed and correct-
        ness.  All must be specifically enabled.

        -ffloat-store
            Do not store floating point variables in registers, and inhibit
            other options that might change whether a floating point value is
            taken from a register or memory.

            This option prevents undesirable excess precision on machines
            such as the 68000 where the floating registers (of the 68881)
            keep more precision than a "double" is supposed to have.  Simi-
            larly for the x86 architecture.  For most programs, the excess
            precision does only good, but a few programs rely on the precise
            definition of IEEE floating point.  Use -ffloat-store for such
            programs, after modifying them to store all pertinent intermedi-
            ate computations into variables.

Wenn ich mit -ffloat-store übersetze, erhalte ich:

z: 4294836225
z1: 18445618199572250624
z2: 18445618199572250624

-- 
Thomas



More information about the Linux-support mailing list