How can we type cast a double number to long. I am getting wrong value, when i am trying to type cast it to long.

int main()
{
int s=9;
double minuttakst=0.019000;
int takst_enhed=60;

double   antal_minutter, beloeb;

   antal_minutter = (double) s  / (double)takst_enhed;

   beloeb = ((double)((long)(((antal_minutter * minuttakst) *  10000.0) + 0.5)
))   /   10000.0;

printf(" \nbeloeb is :%lf\n",beloeb);
return 0;
}

The output of the program is 0.002800, but i should get it as 0.002900

Recommended Answers

All 5 Replies

why are you even using the type long? you know that long is an integer, right?

what you are doing here is taking the double floating point value (antal_minutter * minuttakst * 10000.0 + 0.5 ) , converting it into a long int which drops any fractional part, then converting it back to a double ... and then finally dividing by 10000.

get rid of the long type cast. that's what's hurting you here. Furthermore, since you've already defined everything as a double anyhow, you have no need to do any type casting at all. they already are doubles. this is all you need to do to get your answer:

beloeb = (antal_minutter * minuttakst * 10000.0 + 0.5) / 10000.0

also, you don't need to use the format specifier "%lf" to print a double. "%f" is the correct format specifier. actually i'm not even sure if "lf" means anything. theres an "Lf" format which only works on some compilers and means "long double" ... and that's an 80-bit floating point value, and I seriously doubt you need that, just for these little values.


.

get rid of the long type cast. that's what's hurting you here. Furthermore, since you've already defined everything as a double anyhow, you have no need to do any type casting at all. they already are doubles. this is all you need to do to get your answer:

beloeb = (antal_minutter * minuttakst * 10000.0 + 0.5) / 10000.0

.

How does adding .5 to a double round the value? It simply adds .5 to the value. To round you must drop all digits beyond the needed digit. Using your technique, you need:

beloeb = long((antal_minutter * minuttakst * 10000.0) + 0.5) / 10000.0;

Casting to long to truncate after the addition.

also, you don't need to use the format specifier "%lf" to print a double. "%f" is the correct format specifier. actually i'm not even sure if "lf" means anything.

Yes he does, and yes it does. %f is for float values. A double is a long float, or double the storage space of a float. Therefore, %f is for a single precision floating value, or a float, whereas %lf is for a double precision float, or a double.

Use plain %f with a double with printf(), see
http://msdn.microsoft.com/en-us/library/hf4y5e3w(VS.80).aspx

Mr Sinkula once pointed out (to me) that:
"using %lf with printf is undefined behavior in C90, a common nonstandard extension, and standardized in the uncommonly-implemented C99 as ignoring the l."

Yes he does, and yes it does.

No he doesn't and no it doesn't.

Walt, %lf is only meaningful for *input*... it is undefined for output

And furthermore, I am not attempting to intuit the "whys" and "wherefores" of his formula, whether it's correct or incorrect for his overall purpose. I am only showing "how" to accomplish what he asked: and that's to fix the forumla he has given to provide the correct answer as it is presented.

he shall get the answer he wants -- according to the formula he has presented -- by losing the long type cast.

that is all.

.

okay, Walt, i see what you're saying about casting with type long in order to round

but now you're answering this question not as originally presented, but based on a subsequent question from a future post that did not exist at the time this question was posted.

so, i have to admit I was neither able to see into the future nor able to read the poster's mind.

...

It would also sure reduce a lot of confusion if the POSTER would keep his questions regarding the same problem in the same thread.

.

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.