Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 6th, 2006, 8:00 AM   #11
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
I think even if I do this:

number float;
number = stof (printf("%.2f\n", number));

not sure it stof is string to float, but anyway I would still have all digits with zeros right?

Can I just set it to 2 digits lik 44.23? maybe I have to change it to short or whatever!

thx for the help guys.
hipolito is offline   Reply With Quote
Old Jul 6th, 2006, 8:25 AM   #12
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You can use the round, roundf and roundl functions to round numbers to the nearest integer. Given that, we can use simple multiplication and division to round to x decimal places.
float number[3] = { 89.27218, 0, 0 };
number[1] = round(number[0]); // 89
number[2] = round(number[0] * 100) / 100); // 89.27
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jul 6th, 2006, 11:49 AM   #13
frankish
Hobbyist Programmer
 
frankish's Avatar
 
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0 frankish is an unknown quantity at this point
Another way to do it would be to mask out all but the first 2 digits in the decimal part of the number. This would require knowledge of how a float is stored on your machine.
frankish is offline   Reply With Quote
Old Jul 6th, 2006, 2:40 PM   #14
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
Quote:
But as he says he needs to use it in the program not as a output, is it not possible at the beginning when you make the my_float to also define how many number my_float is supposed to store? I.e my_float=[4] or something?
No. You can do it for some values, but not others. You can do it for constant values, but nothing that changes over time. If you need to use the value for some kind of comparison, set a fudge factor to the appropriate radix and realize that floating-point is a bitch to work with.
#include <stdio.h>
#include <math.h>

int main ( void )
{
  double a = 87.99876;
  double b = 87.98876;
  const double fudge = .01;

  if ( fabs ( a - b ) <= fudge )
    puts ( "Identical with fudge" );
  else
    puts ( "Different" );

  a = 87.99876;
  b = 87.99765;

  if ( fabs ( a - b ) <= fudge )
    puts ( "Identical with fudge" );
  else
    puts ( "Different" );

  return 0;
}
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Jul 6th, 2006, 4:36 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There are actual quantities and then there are representations of quantities and the failings thereof. Grab your hand calculator and compare 1/3*3 with 1*3/3. Everyone knows they're the same (except your calculator). Another kind of problem is that floating point methods store a much larger range of numbers in the same space as integers with a much smaller range. A second's thought will raise suspicions that something had to go, in the tradeoff. Your machine likes to scale numbers using powers of 2, whereas most of us like to scale numbers using powers of 10. Things won't always work out cleanly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 6th, 2006, 5:24 PM   #16
frankish
Hobbyist Programmer
 
frankish's Avatar
 
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0 frankish is an unknown quantity at this point
Quote:
Originally Posted by DaWei
There are actual quantities and then there are representations of quantities and the failings thereof. Grab your hand calculator and compare 1/3*3 with 1*3/3. Everyone knows they're the same (except your calculator). Another kind of problem is that floating point methods store a much larger range of numbers in the same space as integers with a much smaller range. A second's thought will raise suspicions that something had to go, in the tradeoff. Your machine likes to scale numbers using powers of 2, whereas most of us like to scale numbers using powers of 10. Things won't always work out cleanly.
Interesting...
frankish is offline   Reply With Quote
Old Jul 6th, 2006, 7:48 PM   #17
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Quote:
Originally Posted by DaWei
Grab your hand calculator and compare 1/3*3 with 1*3/3.
You sir, need a newer calculator .

*Whips out TI-84*
1/3*3 = 1
1*3/3 = 1

It works even if you store 1/3 in a variable then multiply by 3. Gotta love the TI-84, but your point is still true, its just not the best example.
Game_Ender is offline   Reply With Quote
Old Jul 6th, 2006, 7:54 PM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Grab your xxxxxxxxxxxxxx ten-cent hand calculator...
:o :p
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 10th, 2006, 8:53 AM   #19
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Game_Ender
You sir, need a newer calculator .

*Whips out TI-84*
1/3*3 = 1
1*3/3 = 1

It works even if you store 1/3 in a variable then multiply by 3. Gotta love the TI-84, but your point is still true, its just not the best example.
I'm not going out to buy a TI-84, but I would guess that's because your calculator stores numbers internally to a higher precision than it displays them. What happens if you compute 1/3*3-1?
grumpy is offline   Reply With Quote
Old Jul 17th, 2006, 9:14 AM   #20
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
Well, this all started couse I can mak a number like 83.389999
become a 83.39 (as a string), but when I put it back to float it becomes
83.390002, and that is all I dont want it.

I guess I can not use float, what is the kind I should use for 83.39?
hipolito is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC