c++ - Will different math CPUs yield the same floating point results? -


i'm developing on os portable software has unit tests must work on linux, unix, , windows.

imagine unit test asserts ieee single-precision floating point value 1.26743237e+015f converted string:

void datatypeconvertion_test::testtofloatwide() {     cdatatypeconversion<wchar_t> datatypeconvertion;     float val = 1.26743237e+015f;     wchar_t *valstr = (wchar_t*)datatypeconvertion.tofloat(val);     std::wcout << valstr << std::endl;     int result = wcscmp(l"1.26743e+015", valstr);     cppunit_assert_equal(0, result);     delete [] valstr; } 

my question is: all os , processors convert float string "1.26743e+015" long float ieee? i'm asking since know math cpus may not return accurate results, , wondering if yield different results on different processors may have different hardware implementations of ieee floating point operations internally inside processor architecture.

the answer, sadly, no. conversion of floating point number , arbitrary strings not guaranteed across platforms.

in principle @ least, processors come across conform ieee 754 standard. standard reasonably tight, extent defines floating point arithmetic. can add/subtract/multiple or divide floating point numbers reasonable expectation of getting identical results across platforms @ bit level.

the standard defines conversion , 'character representation'. in principle requires complying implementations compatible has 'wiggle room'. not numbers have produce identical results.

you should aware default precision , format may vary across platforms.

having said that, may able achieve desired results long (a) control width , precision of strings rather leaving default (b) choose precision within maximum available particular format (c) avoid nan , similar.

the article here quite helpful.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -