OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 1:10 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 5:26 am 
Offline
Member
Member

Joined: Wed Nov 10, 2010 10:49 am
Posts: 109
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.


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 5:43 am 
Offline
Member
Member
User avatar

Joined: Tue Mar 24, 2009 8:11 pm
Posts: 1249
Location: Sunnyvale, California
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?


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 5:55 am 
Offline
Member
Member

Joined: Wed Nov 10, 2010 10:49 am
Posts: 109
Hi,
I am using this code:
Code:
                    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:
        unsigned i = 1.8;
       
        DebugPrintf("---%f----",i);

-----------------------------------
Thank you in advance


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 7:39 am 
Offline

Joined: Thu Jan 06, 2011 9:48 am
Posts: 5
Mozo40 wrote:
and this the code that calls the printf to puts the number:
Code:
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.


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 8:11 am 
Offline
Member
Member
User avatar

Joined: Sun Feb 18, 2007 7:28 pm
Posts: 1564
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();}


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 8:17 am 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 8:19 am 
Offline
Member
Member
User avatar

Joined: Tue Mar 24, 2009 8:11 pm
Posts: 1249
Location: Sunnyvale, California
Did you initialize the FPU?


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 10:53 am 
Offline
Member
Member

Joined: Wed Nov 10, 2010 10:49 am
Posts: 109
yes


Top
 Profile  
 
 Post subject: Re: Float to String or Double to string
PostPosted: Tue Feb 08, 2011 1:44 pm 
Offline
Member
Member

Joined: Tue Jun 15, 2010 9:27 am
Posts: 255
Location: Flyover State, United States
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.

_________________
Getting_Started on the wiki
x86 technical information
Interrupt Jump Table
Real Programmers Don't Use Pascal
My open-source projects


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: scippie and 104 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group