Float to String or Double to string
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Float to String or Double to string
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.
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.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Float to String or Double to string
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?
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Re: Float to String or Double to string
Hi,
I am using this code:
and this the code that calls the printf to puts the number:
-----------------------------------
Thank you in advance
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);
Code: Select all
unsigned i = 1.8;
DebugPrintf("---%f----",i);
Thank you in advance
Re: Float to String or Double to string
it should be "float i = 1.8" / "double i = 1.8"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
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
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.
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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Re: Float to String or Double to string
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
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
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Float to String or Double to string
Did you initialize the FPU?
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
-
- Member
- Posts: 255
- Joined: Tue Jun 15, 2010 9:27 am
- Location: Flyover State, United States
- Contact:
Re: Float to String or Double to string
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.