math output on C++ (checked against other language)

Viewed 97

I am writing this in C++:

int main()
{
    cout<<"print "<< int(((float(979430543) - float(800445804))/2.0)+.5);
}

for output: 89492352

and checking against julia language:

print(Int64(((Float64(979430543) - Float64(800445804))/2.0)+.5))
89492370

The difference between the results is 18 - what am i missing?

3 Answers

float in C++ is most likely to be 32 bits, while Float64 is, well, surely 64 bits wide. Change your float casts to double casts in the C++ version and it will surely produce the expected output.

You are not using equivalent data types.

Int64 should translate to std::int64_t and Float64 typically to double.

1.7.2> n = 979430543
979430543

1.7.2> n - Int(Float32(n))
15

1.7.2> n - Int(Float64(n))
0

The Julia result is correct. As others have said, you are using Float32 in your C++ code.

Related