Regarding rounding behavior with "printf("%.0f\n", 2.5);" in C. #5514
-
When we use "printf("%.0f\n", 2.5);", in C, it actually rounds the number instead of truncating. I'm unexpectedly getting different behavior for "2.5" (actually, in regex, "^\d+.\d*5$") across platforms. On my laptop (Windows 11) it rounds down and prints "2". The command I'm using for compiling is the same on all platforms. I was expecting the Windows ones to be rounding down (as on my laptop). It actually does round up on my laptop if I use the options "-std=gnu11" or "-std=gnu99". But it rounds down using "-std=c11" and "-std=c99". No configuration seems to make it round down on GH Actions. I made this repo for testing it: https://github.com/ArielMAJ/GHA_and_C11_Across_Platforms Anyone have any idea if there is an easy way to handle this (to have it round down on windows) without changing the code in the test.c file? I want to make a workflow to automatically compile, run and test c/c++ code submissions and I don't want the people submitting to have to be mindful of this rounding thing. And I wanted it to be tested on different platforms, including windows (which is the one I mainly use). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello @ArielMAJ, It is very interesting question indeed, i've just read the ISO/IEC 9899:2011 (in open edition), namely §7.21.6.1 (p. 309) and it says about rounding, not truncating and it says nothing special about the values in the very middle of range. From the other hand i did some investigating and searches and it looks like not all compilers follow the same rule and in real life use different strategies for the values in the very middle of range (nearest even for example) Seeing different platforms and different versions (9.x and 8.x)of the different toolchains i would not expect the same output. Although you may play with installing the same version of gcc but i would not expect too much from the attempts because gcc frontend may and should use the different compilers backends/stdlib implementations. So, the only answer i can give now is either to use |
Beta Was this translation helpful? Give feedback.
-
Hello @ArielMAJ , do you have any additional info about this ticket? I am going to close it in few next days in case there no more data to investigate, but please feel free to re-open it or open another one in case any problems, thanks! |
Beta Was this translation helpful? Give feedback.
Hello @ArielMAJ,
It is very interesting question indeed, i've just read the ISO/IEC 9899:2011 (in open edition), namely §7.21.6.1 (p. 309) and it says about rounding, not truncating and it says nothing special about the values in the very middle of range.
From the other hand i did some investigating and searches and it looks like not all compilers follow the same rule and in real life use different strategies for the values in the very middle of range (nearest even for example)
Seeing different platforms and different versions (9.x and 8.x)of the different toolchains i would not expect the same output. Although you may play with installing the same version of gcc but i would not expect to…