Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 60
I've tested different compilers and CPU architectures now. It turns out that 0.0/0.0 = -nan is specific to x86-64 CPUs. On ARM CPUs 0.0/0.0 results in positive nan. Might be though that even AMD and Intel CPUs have different behavior. The compiler doesn't make any difference, it's just the CPU that causes this weird behavior. Yet it's interesting that clang optimises 0.0/0.0 to positive nan though the result without optimisations would be -nan.
31st Jan 2020, 2:39 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 13
I know all of them in JavaScript will print them same.. Except 0.0/0.0 : NaN not -NaN // 1/0 infinity 0/0 nan 0/1 0 1/1 1 inf-inf nan inf+inf inf inf+nan nan nan(+-*/)anything nan nan==nan false
31st Jan 2020, 6:30 AM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 11
Sonic I don't think it's the compiler, it's rather the CPU. But I'll have a look on godbolt.org later. ChillPill I can confirm that the signed bit is set to negative. 0.0 however has no negative sign. You can print the float as binary with this code of mine: https://code.sololearn.com/ceat24D85Zxf/?ref=app
31st Jan 2020, 1:19 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 10
ChillPill just shared what I could find most relevant to the question. Neither I understand the logic behind that.
31st Jan 2020, 6:11 AM
Avinesh
Avinesh - avatar
+ 10
For the second part of the question it seems to treat nan like a variable, which is very weird.
31st Jan 2020, 12:52 PM
Sonic
Sonic - avatar
+ 9
My guess is that some compilers always set the sign bit (to negative) when a result is nan, but of course it's a guess. It may be worthwhile testing this on several compilers.
31st Jan 2020, 12:59 PM
Sonic
Sonic - avatar
+ 9
On SL compiler, it's consistently -nan and not probabilistically positive or negative. Was just testing if it could be sometimes positive. https://code.sololearn.com/cGoLP2m1OkGN/?ref=app
31st Jan 2020, 1:05 PM
Sonic
Sonic - avatar
+ 8
Aaron Eberhardt I see. Nice code BTW.
31st Jan 2020, 1:23 PM
Sonic
Sonic - avatar
+ 8
Now the question is what CPUs or Operating systems set the sign bit and which ones don't I guess.
31st Jan 2020, 1:26 PM
Sonic
Sonic - avatar
+ 8
ChillPill It seems like calculations with nan don't follow a certain logic except that they always return positive or negative nan. Probably there really isn't very much logic behind it because the sign of the nan actually doesn't matter. There's simply no reason why a CPU should process bits it doesn't care about (this also explains the weird behavior with 0.0/0.0). The problem somehow is that nan doesn't depend on a sign yet floats have a fixed sign bit that undesirably influences nan, too. I think cout and printf should actually ignore the sign of nan: nan can not have a sign, it's not a number.
31st Jan 2020, 8:28 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 6
Important info: banana :D
31st Jan 2020, 7:10 PM
Jason Stone
Jason Stone - avatar
+ 6
My understanding of NaN is the only part of NaN with any significance is that it’s a NaN, there is no logic to how NaN is created, and it is a mistake to treat the leftmost bit as a sign bit or any other bit of it as having any significance. Whether this matches the specification of NaN or not, I don’t know.
31st Jan 2020, 8:50 PM
Jason Stone
Jason Stone - avatar
+ 6
yeah ChillPill . Nice discovery. i go with Aaron Eberhardt 's explanation. Sometimes different CPU architectures can whine the results. For example in computing as a whole, Arithmetic computation is usually converted to binary and the answer(result) is converted back to human readable format.
2nd Feb 2020, 11:25 AM
Dace [Super Inactive| Challenge=Maybe]
Dace [Super Inactive| Challenge=Maybe] - avatar
+ 5
ChillPill take a look at this. nan is definitely not being treated like a variable there. The answer is positive nan in all these cases. https://code.sololearn.com/cuVaXHfcmFd3/?ref=app
2nd Feb 2020, 11:34 AM
Sonic
Sonic - avatar
+ 4
tushar sahni it's because 0.7 can't be accurately represented by a finite number of bits and because you get a number marginally greater than 0.7.
2nd Feb 2020, 11:28 AM
Sonic
Sonic - avatar
+ 4
Sonic In python floats calculation are wobbly because they need to be converted to binary and the float may not have a binary so the closest is the result
2nd Feb 2020, 12:09 PM
Dace [Super Inactive| Challenge=Maybe]
Dace [Super Inactive| Challenge=Maybe] - avatar
+ 2
Using exceptions
31st Jan 2020, 7:34 PM
Mustinsar Saeed
Mustinsar Saeed - avatar
+ 2
Hi I think it's negative to distinguish number/0 and 0/0 But I'm not sure about this
1st Feb 2020, 12:49 AM
SEYYED MOHAMMAD MAHDI SAJED SAADAT
SEYYED MOHAMMAD MAHDI SAJED SAADAT - avatar
+ 2
Perhaps it's a hyphen and not a negative at all? Also, regarding -nan+2*nan=nan, if all nan's are treated equal, then it makes sense to end as a positive...-nan+nan would cancel eachother out, so add another nan and you've got nan! Seems completely logical to me.
1st Feb 2020, 8:36 AM
Mark McClan
Mark McClan - avatar