954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please help me in Type casting

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

yelugamraghu
Newbie Poster
2 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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 along 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.


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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 doubleround 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 tolong 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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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."

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 
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.

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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.

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You