Will your compiler assign the real number of scientific notion (1.0e-6) as double precision

copyright (C) qingfeng Xia 2011-2020 CC-BY-NC 4.0

Will your compiler assign the real number of scientific notion (1.0e-6) as double precision.

! I was told the  scientific nation (1.0e-6) is only single precision in Fortran; b=1.0d-6  is the corrected double precision representation for double precision!I just wonder if this is true for other languages. here is the test!

Conclusion:   It is true for Fortran, but not C++ or Python

#############################################

! Fortran 90

program main

implicit none

real(kind=8) :: a=1.0e-6

! double precision scientific notion

real(kind=8) :: b=1.0d-6

 

write(*,*) a

write(*,*)  b

end

#################### ##########################

//  C/C++   test in GCC 4

 

#include <stdio.h>

 

void main()

{

printf(“Test one:  Will C compiler assign the scientific notion  1.0e-6 as double precision \n”);

float a =1.0e-6;  // error or warning,   float a = 1.0f;

// float single precision has only 7 significant decimals !

double b=1.0e-6;  // fine, the precision is double

double c=0.000001f;     // same as a,

printf(“float a =  a=%4.8e \n  double b= , b=%4.8e \n float  c=; c=%4.8e \n”, a, b, c);

printf(“float a = %lf \n  double b=%lf \n double  c=%lf \n”, a, b, c);

 

//  “%lf” is need for read double from console

printf(“Test two: the safety and type idenfier for scanf() \n “);

float d;

double e;

printf(“Please input two float number seperated by space and ended by return key \n”);

scanf(“%f  %lf”, &d, &e);   //

printf(“%f  %f”, d, e);      // %f can print double

 

//printf(“Please input two float number seperated by space and ended by return key \n

 

//scanf() is sensitive for the type identifier.  not type-safe!

//  C++ IO is preferred for user input, or read only string and convert to number by your self!

//scanf(“%f  %lf”, &e, &d);    // program exit!

//printf(“%f  %lf”, d, e);     // program exit!

 

// scanf()  makes the conversion from string to number,  if the input is not a valid number? what happen?

double cc;

printf(“Please an invalid number such as 12×5 ended by return key, see what happened \n”);

scanf(“%f “, &cc);    // program exit!

 

}

 

/*

Well, like in printf(), you can add a modifier before the type specifier to tell scanf() that you have a longer or shorter type. The following is a table of the possible modifiers:

 

h       The value to be parsed is a short int or short unsigned. Example: %hd or %hu.

l       The value to be parsed is a long int or long unsigned, or double (for %f conversions.) Example: %ld, %lu, or %lf.

L      The value to be parsed is a long long for integer types or long double for float types. Example: %Ld, %Lu, or %Lf.

*     Tells scanf() do to the conversion specified, but not store it anywhere. It simply discards the data as it reads it. This is what you use if you want scanf() to eat some data but you don’t want to store it anywhere; you don’t give scanf() an argument for this conversion. Example: %*d.

 

*/

######################################################

// C#  Java,

not test!!!

#####################################################

// python, is fine,

>>> repr(1.0e-6)

‘9.9999999999999995e-07’

 

 



CC-BY-NC 4.0 licensed free for non-commercial usage
Author: Qingfeng XIA
copyright (C) 2011-2020
http://www.iesensor.com
please keep the original link in your reference.
http://www.iesensor.com/blog/2011/06/21/will-your-compiler-assign-the-real-number-of-scientific-notion-1-0e-6-as-double-precision/
This entry was posted in Uncategorized. Bookmark the permalink.