Double implicitly converted to real

Charles McAnany mcanance at rose-hulman.edu
Thu Nov 3 07:39:49 PDT 2011


Hi. I noticed that one of the guarantees in TDPL is that any code that is valid in both C
and D should compile with the same result. But I'm seeing a different behavior here.
I'm trying to find the smallest double for which the comparison x+1/x = x is true.
I take a number way too small, and a number way too large, and then binary search (for 100
iterations) to get the desired number:

//add appropriate import std.stdio or #include <stdio.h>
int main(){
    double min = 1;
    double max = 10000000000;
    int iters = 0;
    double average;
    for(;iters<100; iters++){
        average = (min+max)/2;
        if( average + 1/average == average)
            max = average;
        else
            min = average;
    }
    printf("%f",average);
    return 0;
}

Here's the problem:
D (under DMD v2.051) gives this answer: 4294967296.000000
C (gcc version 3.4.6 20060404): 134217728.000000

It seems D is implicitly converting double to real. Is this the usual behavior?

Cheers,
Charles.


More information about the Digitalmars-d-learn mailing list