Matlab vs C++ Double Precision

You got confused by the different ways C++ and MATLAB are printing double values. MATLAB’s format long only prints 15 significant digits while C++ prints 17 significant digits. Internally both use the same numbers: IEEE 754 64 bit floating point numbers. To reproduce the C++-behaviour in MATLAB, I defined a anonymous function disp17 which prints … Read more

Does any floating point-intensive code produce bit-exact results in any x86-based architecture?

Table of contents: C/C++ asm Creating real-life software that achieves this. In C or C++: No, a fully ISO C11 and IEEE-conforming C implementation does not guarantee bit-identical results to other C implementations, even other implementations on the same hardware. (And first of all, I’m going to assume we’re talking about normal C implementations where … Read more

Why does division by zero in IEEE754 standard results in Infinite value?

It’s a nonsense from the mathematical perspective. Yes. No. Sort of. The thing is: Floating-point numbers are approximations. You want to use a wide range of exponents and a limited number of digits and get results which are not completely wrong. 🙂 The idea behind IEEE-754 is that every operation could trigger “traps” which indicate … Read more

Portability of binary serialization of double/float type in C++

Brian “Beej Jorgensen” Hall gives in his Guide to Network Programming some code to pack float (resp. double) to uint32_t (resp. uint64_t) to be able to safely transmit it over the network between two machine that may not both agree to their representation. It has some limitation, mainly it does not support NaN and infinity. … Read more

Algorithm to convert an IEEE 754 double to a string?

The code used by various software environments to convert floating-point numbers to string representations is typically based on the following publications (the work by Steele and White is particularly frequently cited): Jerome T. Coonen: “An Implementation Guide to a Proposed Standard for Floating-Point Arithmetic.” Computer, Vol. 13, No. 1, January 1980, pp. 68-79 Guy. L. … Read more

Python float – str – float weirdness

str(0.47000000000000003) give ‘0.47’ and float(‘0.47’) can be 0.46999999999999997. This is due to the way floating point number are represented (see this wikipedia article) Note: float(repr(0.47000000000000003)) or eval(repr(0.47000000000000003)) will give you the expected result, but you should use Decimal if you need precision.

tech