HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Issue in converting character string to float,...
Operating System - HP-UX
1837976
Members
2802
Online
110124
Solutions
Forums
Categories
Company
Local Language
back
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
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
11-17-2006 12:07 AM
11-17-2006 12:07 AM
Hi,
The below mentioned code is running on a HP-UX machine and is giving output as shown below. I could not figure out the reason for getting [641.330017] instead of [641.330000].
Can some body please point out the reason for this?
Thanks,
Saurav
Code:
#include
#include
int main(void)
{
char *cpStr = "00000641.33";
float f = 0.0;
double d = 0.0;
double e = 0.0;
float g = 0.0;
f = (float)atof(cpStr);
d = (double)atof(cpStr);
e = atof(cpStr);
g = atof(cpStr);
printf("\n cpStr = [%s] f = [%f] d = [%f] [e] = [%f] [g] = [%f] ... \n\n",cpStr,f,d,e,g);
e = 641.33;
f = (float)e;
printf("\n [e] = [%f] f = [%f] ... \n\n",e,f);
exit(0);
}
Output :
cpStr = [00000641.33] f = [641.330017] d = [641.330000] [e] = [641.330000] [g] = [641.330017] ...
[e] = [641.330000] f = [641.330017] ...
The below mentioned code is running on a HP-UX machine and is giving output as shown below. I could not figure out the reason for getting [641.330017] instead of [641.330000].
Can some body please point out the reason for this?
Thanks,
Saurav
Code:
#include
#include
int main(void)
{
char *cpStr = "00000641.33";
float f = 0.0;
double d = 0.0;
double e = 0.0;
float g = 0.0;
f = (float)atof(cpStr);
d = (double)atof(cpStr);
e = atof(cpStr);
g = atof(cpStr);
printf("\n cpStr = [%s] f = [%f] d = [%f] [e] = [%f] [g] = [%f] ... \n\n",cpStr,f,d,e,g);
e = 641.33;
f = (float)e;
printf("\n [e] = [%f] f = [%f] ... \n\n",e,f);
exit(0);
}
Output :
cpStr = [00000641.33] f = [641.330017] d = [641.330000] [e] = [641.330000] [g] = [641.330017] ...
[e] = [641.330000] f = [641.330017] ...
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2006 12:24 AM
11-17-2006 12:24 AM
SolutionSingle float is stored in just 32 bits. Ther is only so much precision you get with that.
Roughly that precision is 9 places, but not all number can be stored exactly. Thus the 'odd rounding'.
Read teh following carefully:
http://docs.hp.com/en/B3906-90006/ch02s02.html
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2006 01:25 PM
11-17-2006 01:25 PM
Re: Issue in converting character string to float, double.
>Hein: Thus the 'odd rounding'.
Right.
If you asked printf to round to only 2 digits, it would print it "better":
printf("f = [%.2f]\n",f);
f = [641.33]
If you want to see what's in a float vs a double, you could use the %a format on IPF to print it in hex.
printf("f = [%.2f] f = [%a] d = [%a] \n",f,f,d);
f = [641.33] f = [0x1.40aa3e0000000p+9] d = [0x1.40aa3d70a3d71p+9]
Right.
If you asked printf to round to only 2 digits, it would print it "better":
printf("f = [%.2f]\n",f);
f = [641.33]
If you want to see what's in a float vs a double, you could use the %a format on IPF to print it in hex.
printf("f = [%.2f] f = [%a] d = [%a] \n",f,f,d);
f = [641.33] f = [0x1.40aa3e0000000p+9] d = [0x1.40aa3d70a3d71p+9]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2006 04:11 PM
11-24-2006 04:11 PM
Re: Issue in converting character string to float, double.
Thanks you.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP