Float to String or Double to string

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Float to String or Double to string

Post 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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Float to String or Double to string

Post 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?
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: Float to String or Double to string

Post 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
prasadjvv
Posts: 5
Joined: Thu Jan 06, 2011 9:48 am

Re: Float to String or Double to string

Post 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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Float to String or Double to string

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: Float to String or Double to string

Post 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
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Float to String or Double to string

Post by NickJohnson »

Did you initialize the FPU?
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: Float to String or Double to string

Post by melgmry0101b »

yes
Tosi
Member
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

Post 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.
Post Reply