int to float to float to int

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

Join Date: Jul 2009
Posts: 36
Reputation: yasaswyg has a little shameless behaviour in the past 
Solved Threads: 0
yasaswyg yasaswyg is offline Offline
Light Poster

int to float to float to int

 
0
  #1
Jul 27th, 2009
i need help
I need to display
Example
23.4
and display 23 and .4

how do i do this?

so i need to convert a decimal into fraction
like this
http://www.cimt.plymouth.ac.uk/proje...7/bk7_17i1.htm
i need to display 4 and 0.276
Last edited by yasaswyg; Jul 27th, 2009 at 4:48 pm. Reason: found out
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: int to float to float to int

 
0
  #2
Jul 27th, 2009
I need to display
Example
23.4
and display 23 and .4
One easy way is with the type system. When you cast a floating point value it cuts off the precision. If you subtract that result from the original, you get the precision:
  1. double d = 23.4;
  2. double w = (int)d; /* w == 23.0 */
  3. double p = d - w; /* p == 0.4 */
Last edited by Tom Gunn; Jul 27th, 2009 at 5:01 pm.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: int to float to float to int

 
0
  #3
Jul 27th, 2009
just do type casting
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: yasaswyg has a little shameless behaviour in the past 
Solved Threads: 0
yasaswyg yasaswyg is offline Offline
Light Poster

Re: int to float to float to int

 
0
  #4
Jul 27th, 2009
Originally Posted by Tom Gunn View Post
One easy way is with the type system. When you cast a floating point value it cuts off the precision. If you subtract that result from the original, you get the precision:
  1. double d = 23.4;
  2. double w = (int)d; /* w == 23.0 */
  3. double p = d - w; /* p == 0.4 */
DAMN thats common sense
THanks alot.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: yasaswyg has a little shameless behaviour in the past 
Solved Threads: 0
yasaswyg yasaswyg is offline Offline
Light Poster

Re: int to float to float to int

 
0
  #5
Jul 27th, 2009
idk what that means
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: int to float to float to int

 
0
  #6
Jul 28th, 2009
you dont know what what means? casting? start from the beginning of Tom Gunns code snippet:

'd' is a double (floating point) variable containing 23.4.

'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.

'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'

now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)


.
Last edited by jephthah; Jul 28th, 2009 at 12:32 am. Reason: .
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: yasaswyg has a little shameless behaviour in the past 
Solved Threads: 0
yasaswyg yasaswyg is offline Offline
Light Poster

Re: int to float to float to int

 
0
  #7
Jul 28th, 2009
im sorry but i havent learned that in my class

Originally Posted by jephthah View Post
you dont know what what means? casting? start from the beginning of Tom Gunns code snippet:

'd' is a double (floating point) variable containing 23.4.

'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.

'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'

now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)


.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: 42Wired is an unknown quantity at this point 
Solved Threads: 0
42Wired 42Wired is offline Offline
Newbie Poster

Re: int to float to float to int

 
0
  #8
Aug 17th, 2009
Originally Posted by yasaswyg View Post
i need help
I need to display
Example
23.4
and display 23 and .4

how do i do this?
I'm trying to do something similar, only instead of displaying the numbers, I need to store them. I need to store both as ints, though.

The problem I'm running into is coming up with a generic algorithm for storing the 0.4 part as just 4. I don't know how many digits the number will be every time, so I can't use a constant multiplier to move the decimal.

This is what I've come up with so far:

float n = 123.456;

// 123
int nw = (int)n;

// 456
int nf = (int)( ( n - nw ) * pow( 10, ??? ) );

I would much prefer a mathematical solution, but I would be willing to convert to a string and back, if that's the only way.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 66
Reputation: Protuberance will become famous soon enough Protuberance will become famous soon enough 
Solved Threads: 14
Protuberance's Avatar
Protuberance Protuberance is offline Offline
Junior Poster in Training

Re: int to float to float to int

 
0
  #9
Aug 17th, 2009
Floating type does not have fixed precision.
and if you enter
123.456
try to print that variable - you'll get something like that
123.455969706597869578956
So you have to convert its number into string and parse it.

Or choose precision and your code will be like that
  1. int prec = 5;
  2. int nf = (int)( ( n - nw ) * pow( 10, prec ) );
But this method is not precise.
Last edited by Protuberance; Aug 17th, 2009 at 2:06 pm.
"If a problem can be decided - it is not needed to worry, and if to decide it is impossible - worrying is useless." (с)
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: 42Wired is an unknown quantity at this point 
Solved Threads: 0
42Wired 42Wired is offline Offline
Newbie Poster

Re: int to float to float to int

 
0
  #10
Aug 17th, 2009
Is there a better (faster) way to convert than sprintf() that you know of? I'm working in a real-time system, and sprintf() is computationally expensive.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC