| | |
Please help me in Type casting
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
The output of the program is 0.002800, but i should get it as 0.002900
c Syntax (Toggle Plain Text)
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; }
Last edited by Narue; May 20th, 2008 at 5:09 pm. Reason: Added code tags
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
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:
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.
.
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:
C Syntax (Toggle Plain Text)
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.
•
•
•
•
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:
.C Syntax (Toggle Plain Text)
beloeb = (antal_minutter * minuttakst * 10000.0 + 0.5) / 10000.0
C Syntax (Toggle Plain Text)
beloeb = long((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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 982
Reputation:
Solved Threads: 210
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."
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."
•
•
•
•
Yes he does, and yes it does.
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.
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.
.
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.
![]() |
Similar Threads
- help .. using type casting ? (C++)
- Problem while Type Casting (C++)
- Type casting and type conversions (C)
- Type casting (C++)
- type casting (C++)
- conversion type (C++)
Other Threads in the C Forum
- Previous Thread: Please help me in rounding in C
- Next Thread: Save file in C?
Views: 1446 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user visualstudio voidmain() wab win32 win32api windows.h






