- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- round float to N decimal places
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2003 06:40 AM
07-16-2003 06:40 AM
round float to N decimal places
For example... this doesn't work:
float x=3.1234;
float y;
int aux;
y=x*100.0;
aux=(int) y;
y= ((float)aux)/100.0;
printf("%f\n",y);
After this, printf shows 3.12, but if you use any debugger, it'll show 3.119999... and so on...
any ideas??
Regards,
Mario
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2003 06:50 AM
07-16-2003 06:50 AM
Re: round float to N decimal places
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2003 07:00 AM
07-16-2003 07:00 AM
Re: round float to N decimal places
#include
#ifndef FALSE
#define FALSE (0)
#endif
#define X_BIAS0 1.0e-5
double dblround(double x, int places)
{
if (places >= 0 && x != 0.0)
{
int i = 0,is_neg = FALSE;
double x_bias = X_BIAS0;
char sx[320];
for (i = 1; i <= places; ++i) x_bias /= 10.0;
is_neg = (x < 0.0);
if (is_neg) x = -(x);
x += x_bias;
(void) sprintf(sx,"%.*f",places,x);
x = atof(sx);
if (is_neg) x = -(x);
if (x == 0.0) x = 0.0; /* not as stupid as it seems; when x == 0.0
x = -x causes printf to print -0.0 */
}
return(x);
} /* dblround */
int main()
{
double x1 = 5.0499999,y1 = 0.0;
y1 = dblround(x1,2);
(void) printf("%.4lf\n",y1);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2003 07:32 AM
07-16-2003 07:32 AM
Re: round float to N decimal places
I've heard some folks talking about using BCD here... Personally I'd hate to use it...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2003 07:59 AM
07-16-2003 07:59 AM
Re: round float to N decimal places
e.g.
if (fabs(x - y) <= 1.0e-6)
{
printf("X and Y are equal\n");
}
If you need exact representation then by definition, you can't use floating point.
One option to explore is long long's (64-bit integers) which will probably give you sufficient range, exact representation, and the standard operators *, /, %, and == work without haviung to do everything as functions --- which a BCD approach would require.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2003 08:10 AM
07-16-2003 08:10 AM
Re: round float to N decimal places
Typically folks just use float values that are close enough, then format final results as strings with the desired precision. That usually works fine.
As you noted, BCD or Binary Coded Decimal, represents decimal fractions exactly. It actually uses three bits for each decimal position. All math on BCD numbers must be done with slow software algorithms.
If you are only representing fairly small numbers and doing simple math on them, then you could use a variant of 'fixed precision'. Just keep all your numbers scaled by 100 and then use a shifted decimal point when you format them as strings. Of course simple operations like multiplies and divides would require an adjusting scaling by 100. I can't think of a sound reason to insist on an exact number of decimal places if you start getting into any computation that would involve loss of precision. If you do anything like a divide by three, then more precision is a good thing compared to 'exact' truncation at a short number of decimal places.