Please help me in Type casting

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 2
Reputation: yelugamraghu is an unknown quantity at this point 
Solved Threads: 0
yelugamraghu yelugamraghu is offline Offline
Newbie Poster

Please help me in Type casting

 
0
  #1
May 20th, 2008
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.
  1. int main()
  2. {
  3. int s=9;
  4. double minuttakst=0.019000;
  5. int takst_enhed=60;
  6.  
  7. double antal_minutter, beloeb;
  8.  
  9. antal_minutter = (double) s / (double)takst_enhed;
  10.  
  11. beloeb = ((double)((long)(((antal_minutter * minuttakst) * 10000.0) + 0.5)
  12. )) / 10000.0;
  13.  
  14. printf(" \nbeloeb is :%lf\n",beloeb);
  15. return 0;
  16. }
The output of the program is 0.002800, but i should get it as 0.002900
Last edited by Narue; May 20th, 2008 at 5:09 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Please help me in Type casting

 
0
  #2
May 20th, 2008
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:

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


.
Last edited by jephthah; May 20th, 2008 at 2:16 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,133
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Please help me in Type casting

 
0
  #3
May 22nd, 2008
Originally Posted by jephthah View Post
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:

  1. 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:
  1. beloeb = long((antal_minutter * minuttakst * 10000.0) + 0.5) / 10000.0;
Casting to long to truncate after the addition.


Originally Posted by jephthah View Post
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.
Last edited by WaltP; May 22nd, 2008 at 2:21 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 982
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Please help me in Type casting

 
0
  #4
May 22nd, 2008
Use plain %f with a double with printf(), see
http://msdn.microsoft.com/en-us/libr...3w(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."
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Please help me in Type casting

 
0
  #5
May 22nd, 2008
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.



.
Last edited by jephthah; May 22nd, 2008 at 10:27 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Please help me in Type casting

 
0
  #6
May 22nd, 2008
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.



.
Last edited by jephthah; May 22nd, 2008 at 12:50 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1446 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC