Floating point constant


For the following code:

#include<stdio.h> int main() { float a = 0.7; printf("%.10f %.10f\n", 0.7f, a); return 0; }

The output I get is:


0.7000000000 0.6999999881


Please explain why a is printed as 0.6999999881 while the literal constant is printed as 0.7000000000 ?

Is the use of a floating point constant in this case compiler-dependent?


Obtaining “0.7000000000” as the string printed for printf("%.10f",0.7f) is normal behavior if the compiler defines FLT_EVAL_METHOD as 1 or 2.

Indeed, in that mode, floating-point constants can be represented at a precision <a href="http://port70.net/~nsz/c/c11/n1570.html#" rel="nofollow">beyond</a> that of their type (C11


Except for assignment and cast (which remove all extra range and precision), the values yielded by operators with floating operands and values subject to the usual arithmetic conversions and of floating <strong>constants</strong> are evaluated to a format whose range and precision may be greater than required by the type.


In other words, printing 0.7000000000 0.6999999881 FLT_EVAL_METHOD=2 <a href="http://ideone.com/VGHNjx" rel="nofollow">is one possible behavior</a> for the modified program below.

#include<stdio.h> #include <float.h> int main() { float a=0.7; printf("%.10f %.10f FLT_EVAL_METHOD=%d\n",0.7f, a, (int)FLT_EVAL_METHOD); return 0; }

Because the compiler defines FLT_EVAL_METHOD to 2, printf("%.10f %.10f", 0.7f, 0.7) is treated as if it was printf("%.10f %.10f", (double)0.7L, (double)0.7L). Similarly, printf("%.60Lf %.60Lf\n", (long double)0.7f, (long double)0.7) is equivalent to printf("%.60Lf %.60Lf\n", 0.7L, 0.7L).


The reason you get different values is that, as you are passing to printf(3) (which is a vararg function) as an unchecked parameter, the compiler <em>does</em> a cast to (double) (and an optimization i'm guessing) converting the 0.7f literal to a 0.7D <em>before passing it to printf</em>, so you are actually passing <strong>two different values to printf</strong>, the first is (double)0.7f, and the second is the stored value of variable a, converted to a (double).


  • Why commit does not cause deadlock
  • Jsoup simple HTML tag splitting
  • race condition in mysql select sql
  • Definition of server-class machine changed recently?
  • Will cin recognize \\n typed in from keyboard as a newline character?
  • How to avoid using dynamic variables / a billion if statements in Java?
  • Git subtree post hook
  • How to create a data template dependent on an XML Attribute?
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • Debugging Android native application using ndk-gdb
  • Is it possible to define rest argument in OCaml?
  • Compare struct to a constant in C
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Updating Dojo provide
  • Jquery Knockout: ko.computed() vs classic function?
  • Struts 2 TextField Tag with an attribute and no value
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Android application: how to use the camera and grab the image bytes?
  • How to handle images sent by a mobile device?
  • How to get Eclipse Oxygen to run on Java 9
  • Recording logins for password protected directories
  • Pass value from viewmodel to script in zk
  • QLineEdit password safety
  • Android screen density dpi vs ppi
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • C# - Getting references of reference
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • need help with bizarre java.net.HttpURLConnection behavior
  • apache spark aggregate function using min value
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • JaxB to read class hierarchy
  • Sorting a 2D array using the second column C++