Page 1 of 1
Float to String or Double to string
Posted: Tue Feb 08, 2011 5:26 am
by melgmry0101b
Hi everyone,
Now i am working on a calculator.
I can change integer to string.
But now i have a number like 1.8 , How can i change it to string?
------------********---------------
I tried many times to make a converter.
------------********---------------
i tried to divide the number to three parts :
the first part contains number 1
the second part contains the decimal point
the third part contains the number 8
then change all parts from integer to string.
But this way didn't run.
------------********---------------
Can anyone tell me the right way ? , Please
-------------------------------------------
Thank you in advance.
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 5:43 am
by NickJohnson
The method you described is how I do it (by splitting the number into an integer and fractional part, and optionally an exponent) and it works fine for me. What specific issue are you having with it?
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 5:55 am
by melgmry0101b
Hi,
I am using this code:
Code: Select all
case 'F':
case 'f': {
unsigned* c = va_arg (args, unsigned*);
int* part1;
int* part2;
if (c == 0) {
DebugPuts("0.0");
}
else {
for (int y = 0 ; y < 70 ; y++) {
if (c[1] == '-') {
DebugPuts("-");
}
if (c[y] == '.') {
for (int f = 0 ; f < y - 1 ; f++){
if (c[1] == '-'){
//do not do any thing :)
}
else{
part1[f] = (int)c[f];
}
}
for (int f = 0 ; f < y + 1 ; f++){
part2[f]= (int)c[f];
}
break;
}
}
char* tt;
itoa_s ((int)part1,10,tt);
DebugPuts(tt);
itoa_s ((int)part2,10,tt);
DebugPuts(tt);
and this the code that calls the printf to puts the number:
Code: Select all
unsigned i = 1.8;
DebugPrintf("---%f----",i);
-----------------------------------
Thank you in advance
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 7:39 am
by prasadjvv
Mozo40 wrote:
and this the code that calls the printf to puts the number:
Code: Select all
unsigned i = 1.8;
DebugPrintf("---%f----",i);
-----------------------------------
Thank you in advance
it should be "float i = 1.8" / "double i = 1.8"
I am sorry to say you, but I strongly suggest you to learn basics first before progressing.
I found the following mistakes in your code.
1. using uninitialized pointer
part1[f] = (int)c[f];
2. looks like you are not very familier with va_arg (or variable number of arguements)
case 'f':
{
unsigned* c = va_arg (args, unsigned*);
/* you are intended receive a double (not float too) value here for "%f".
as the format specifier f stands for - Print a double in normal (fixed-point) notation */
3. trying to display something in a loop.
for (int y = 0 ; y < 70 ; y++)
{
if (c[1] == '-')
{
DebugPuts("-");
}
...
}
intentionally doing this? then ignore my comment.
Thanks
Prasad JVV.
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 8:11 am
by neon
Hello,
You cannot parse a floating point number like a string and expect it to work.
Follow the IEEE 754-2008 standard to extract the decimal and fraction part from the number, then you can use itoa to convert those parts to strings. Please do this after fixing the errors mentioned in the above post.
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 8:17 am
by melgmry0101b
I know that "double i = 1.8"
and i know that i must receive double from va_arg
the problem is :
when i want to use double or float i get general protection fault #GPF
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 8:19 am
by NickJohnson
Did you initialize the FPU?
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 10:53 am
by melgmry0101b
yes
Re: Float to String or Double to string
Posted: Tue Feb 08, 2011 1:44 pm
by Tosi
Is your compiler generating code that makes use of SIMD instructions? If your target is a newer CPU and you have optimizations on, this is possible. There are some more things you must setup for SSE to work, read the Intel manuals for more information.