473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

parsing float number

dkk
Here is the code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float f;
int part1, part4;
float part2;
char part3[6];
char s[]="1234.56abc 903";
char *p;

sscanf(s, "%2d%5e%5s%d", &part1, &part2, &part3, &part4);
printf("parse string: %s\n", s);
printf("part1=%d\n", part1);
printf("part2=%f\n", part2);
printf("part3=%s\n", part3);
printf("part4=%d\n", part4);

return 0;
}

Here is the result:
parse string: 1234.56abc 903
part1=12
part2=34.560001
part3=abc
part4=903

question is why part2 is 34.560001, I expect this number to be 34.56.
Is there a way I can get exact 34.56?
Thanks.

Apr 11 '06 #1
10 2525
dkk opined:
Here is the code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float f;
This is not used.
int part1, part4;
float part2;
Using `double` is (almost) always better;
char part3[6];
char s[]="1234.56abc 903";
char *p;
This is not used.

sscanf(s, "%2d%5e%5s%d", &part1, &part2, &part3, &part4);
You need to pass `part3`, not `&part3` here.
printf("parse string: %s\n", s);
printf("part1=%d\n", part1);
printf("part2=%f\n", part2);
printf("part3=%s\n", part3);
printf("part4=%d\n", part4);

return 0;
}

Here is the result:
parse string: 1234.56abc 903
part1=12
part2=34.560001
part3=abc
part4=903

question is why part2 is 34.560001, I expect this number to be 34.56.
Is there a way I can get exact 34.56?


If you use `double` for `part3` (and change `sscanf()` call to use
"%5le" instead of "%5e") you will get what you're looking for -- in
this particular case. However, bear in mind that not all numbers are
exactly representable in floating point arithmetic, hence you'll
always find cases where what is stored is not what you expect.

If you really, really need exact representation, look for libraries
that support infinite precision.

--
"MSDOS didn't get as bad as it is overnight -- it took over ten years
of careful development."
(By dm******@aix1.uottawa.ca)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 11 '06 #2
dkk wrote:
Here is the code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float f;
int part1, part4;
float part2;
char part3[6];
char s[]="1234.56abc 903";
char *p;

sscanf(s, "%2d%5e%5s%d", &part1, &part2, &part3, &part4);
printf("parse string: %s\n", s);
printf("part1=%d\n", part1);
printf("part2=%f\n", part2);
printf("part3=%s\n", part3);
printf("part4=%d\n", part4);

return 0;
}

Here is the result:
parse string: 1234.56abc 903
part1=12
part2=34.560001
part3=abc
part4=903

question is why part2 is 34.560001, I expect this number to be 34.56.
Is there a way I can get exact 34.56?
Thanks.

If this is some specific case - then...

printf("part2=%.2f\n", part2);

and see Vladimir's comments!

--
==============
Not a pedant
==============
Apr 11 '06 #3
dkk

excellent. It worked. Thanks guys.

Apr 11 '06 #4
dkk opined:
excellent. It worked. Thanks guys.


What worked? Please quote context (and read link in my sig).

--
The only "intuitive" interface is the nipple. After that, it's all
learned.
(Bruce Ediger, be*****@teal.csn.org, in comp.os.linux.misc, on X
interfaces.)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 11 '06 #5
dkk wrote:
question is why part2 is 34.560001, I expect this number to be 34.56.
Is there a way I can get exact 34.56?


Until you can find a fraction of the form a / b = 34.56 where a and b
are integers and b is a power of the radix used in your implementation
(typically 2), not usually. This is one of the reasons that some
implementations of some languages use (or provide) BCD floating point.
Apr 11 '06 #6
>
question is why part2 is 34.560001, I expect this number to be 34.56.
Is there a way I can get exact 34.56?
Thanks.


Not in standard "C". Usually "C" represents floating point as a binary
fraction. Just as you can't represent 1/3 as a decimal, you can't represent
all decimals as binary fractions. Try using a language such as REXX or COBOL
that supports decimal arithmetic.
Apr 11 '06 #7
dkk wrote:
Here is the code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float f;
int part1, part4;
float part2;
char part3[6];
char s[]="1234.56abc 903";
char *p;

sscanf(s, "%2d%5e%5s%d", &part1, &part2, &part3, &part4);
printf("parse string: %s\n", s);
printf("part1=%d\n", part1);
printf("part2=%f\n", part2);
printf("part3=%s\n", part3);
printf("part4=%d\n", part4);

return 0;
}

Here is the result:
parse string: 1234.56abc 903
part1=12
part2=34.560001
part3=abc
part4=903

question is why part2 is 34.560001, I expect this number to be 34.56.
Is there a way I can get exact 34.56?
Thanks.

The "%f" defaults to six decimals fraction part, no matter the 'truth'.
If you want only two decimals, "%.2f" does the trick.

While we're at it, note that floating point is represented in your
computer in binary, not decimal. Conversions between binary and decimal
are subject sometimes to minor error.

Integers (within range) and reals with fractional parts powers of 2 may
be evaluated the same in binary and decimal. In FP we have a binary
point (not decimal point) and .1 means one half and .01 means one
quarter, .001 is one eighth. For example all of decimal..

x.125
x.25
x.5
x.75
x.625
x.875

... can be exactly converted to binary. Decimal x.1 cannot be exactly
represented in FP binary. It gets real close.

I think I'll stop this now and write a book.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Apr 12 '06 #8

dkk wrote:
Here is the code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float f;
int part1, part4;
float part2;
char part3[6];
char s[]="1234.56abc 903";
char *p;

sscanf(s, "%2d%5e%5s%d", &part1, &part2, &part3, &part4);
printf("parse string: %s\n", s);
printf("part1=%d\n", part1);
printf("part2=%f\n", part2);
printf("part3=%s\n", part3);
printf("part4=%d\n", part4);

return 0;
}

Here is the result:
parse string: 1234.56abc 903
part1=12
part2=34.560001
part3=abc
part4=903

question is why part2 is 34.560001, I expect this number to be 34.56.
Is there a way I can get exact 34.56?
Thanks.

you can try this:
sscanf("%2d%5.2lf%5s%d",&part1, &part2,part3, &part4);
then:
printf("part1:%2d\npart2:%5.2lf\npart3:%5s\npart4: %d\n",part1,
&part2,part3, &part4);
the result is what you want.

Apr 12 '06 #9
this document can help you!
url = http://blog.csdn.net/sjf331/archive/...07/339254.aspx

Apr 12 '06 #10
je****@56.com writes:
this document can help you!
url = http://blog.csdn.net/sjf331/archive/...07/339254.aspx


Only if you can read Chinese.

Oh, and please read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 12 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tuang | last post by:
I've been looking all over in the docs, but I can't figure out how you're *supposed* to parse formatted strings into numbers (and other data types, for that matter) in Python. In C#, you can say...
6
by: Ian McConnell | last post by:
I've got a header file which lists a whole load of C functions of the form int func1(float *arr, int len, double arg1); int func2(float **arr, float *arr2, int len, double arg1, double arg2); ...
3
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
1
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
2
by: Rene | last post by:
The following instruction: float myFloat = float.Parse("30.900665"); Assigns a value to myFloat of "30.9006653", how can this be? Where did the extra 3 came from? Thanks
1
by: David Hirschfield | last post by:
Anyone out there use simpleparse? If so, I have a problem that I can't seem to solve...I need to be able to parse this line: """Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));""" with this...
3
by: tokcy | last post by:
Hi everyone, I have class and whenever i am running to this class it shows error like Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.