- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sprintf in c compiles
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
02-20-2002 11:17 AM
02-20-2002 11:17 AM
sprintf in c compiles
sprintf(xxx, "%.2lf",doublefield);
when doublefield=0.735
obtains:
0.74 under 10.20
0.73 under 11.0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2002 11:21 AM
02-20-2002 11:21 AM
Re: sprintf in c compiles
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2002 11:28 AM
02-20-2002 11:28 AM
Re: sprintf in c compiles
Welcome to floating-point math; I'm sure that you are aware either value is equally correct but in most cases you want to round up. Anytime, I'm doing any floating-point stuff, I assume this is going to happen and allow for it.
Typically, you want to add a small bias to the value before rounding.
e.g.
#define X_BIAS 0.0050000001
sprintf(xxx,"%.2lf",doublefield + X_BIAS);
Obviously it's a bit more complicated because you also need to handle negative amount so that generally a function is in order.
BTW: NEVER,EVER compare floating points for equality but rather do something like this:
if (atof(x - y) <= X_BIAS))
printf("x equals y");
else printf ("x not equal to y");
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2002 11:40 AM
02-20-2002 11:40 AM
Re: sprintf in c compiles
#include
main()
{
float doublefield = 0.735;
printf("%.2lf",doublefield);
exit(0);
}
Clay,
Years ago I had to calculate what was deemed to be "close enough" (for a banking app), and of course that required me to learn Logarithms AGAIN! YUCK!!!!
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2002 12:06 PM
02-20-2002 12:06 PM
Re: sprintf in c compiles
I'm an idiot. In my equality test I should have used fabs() rather than atof().
if (fabs(x - y) <= X_BIAS))
printf("x equals y");
else printf ("x not equal to y");
In any event, you really need to use the bias method because you are probably also going to see this problem (feature?) if you need to port your code to another platform/OS. Anytime you are doing floating-point calculations, you simply must take this feature into account.