Member Avatar for marksmania

Hi there,
Think this is a pretty simple problem! I'm trying to create a header file with some physical constants. The Planck constant is giving me trouble due its tiny size in SI units. Essentially, defining a pre-processor constant using E-notation does not seem to work. My header file looks something like this:

#define PI 3.141592654
#define C 299792458.0
#define H 6.26068E-34

I have written a simple program to test if this works:

#include <stdio.h>
#include <math.h>
#include "constants.h"

double a;
int main()
{
a=H;
printf("%lf",a);
}

The output is 0.00000. Not Planck's constant! Any ideas?

Member Avatar for iamthwee

Tell me, what do you expect to get?

I could tell you to go read the manual page for printf to find the ACTUAL conversion for printing a double.

Note that printf and scanf are NOT perfectly symmetrical with their use of conversion formats. %lf for printf is meaningless.

Member Avatar for marksmania

I expected to get 6.626068e-34. And If I remove the "E-34", the program prints 6.626068 just fine, so I think the rest of my code is OK....

Member Avatar for iamthwee

[duplicate]

Member Avatar for marksmania

. %lf for printf is meaningless.

Not sure what you mean by this since using %lf seems to work just fine for any other floating point value....

Use "%e" in printf. And what do you even think "%1f" is doing for your code?

It's been printing the number correctly all along. Since there are 33 zeroes after the decimal point before the 626 part, you get (approximately) zero! Use "%e" to print the number in exponential notation, or "%g" to print it in whichever is more compact. As for "%lf" it is best not to use it, since it is equivalent to "%f" in printf (but not in scanf).

Only the IEEE notation of floating point is able enough to hold the planks constant. As it is a very small number and the first non zero number comes only after nearly 33 digits and so your number gets truncated to first 6 places only. Therefore 0.000000 and as soon as you remove the exponential value it becomes a normal floating point number and hence gets displayed.
Try %e for exponential display but you need to separate the exponential part and involve it separately or else even with all your effort answer will just be 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.