Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
float conversion logic
Author Message
Insectoid

Posted: Fri Dec 19, 2008 12:04 pm   Post subject: float conversion logic

So, I just want some confirmation on my float conversion logic.

I am writing a program to convert a float to another number system. I have managed to separate the float into 2 parts (5.9 => 5, 9). My method for converting the decimal is to flip it over (053 => 350), then convert it in the same manner as an integer, using div and mod, but attach the result backwards again. Is this logic correct? so 50.05 converted to hex would be 32.23?

code, with comments at key points:

Now, either my logic is wrong or I have executed it wrong, or it is correct and I just don't know it. The difficulty is, I don't know what the correct answer is, and it's hard to check, because there aren't too many float converters out there (lots of integer ones though).

OneOffDriveByPoster

Posted: Fri Dec 19, 2008 12:46 pm   Post subject: Re: float conversion logic

50.05 in hex = ?

50 in hex: 32

.05 in hex = ?

5 / 100
5h / 64h

Long division
 code: 0.0CCC... 64    5 00         500

You are looking for 32.0CCC...
Tyr_God_Of_War

Posted: Fri Dec 19, 2008 9:03 pm   Post subject: RE:float conversion logic

You know, the calculator that comes with windows can't do that. It just rounds. That ruins my cunning plan of using it to check your work. Maybe another, better calculator can...
Insectoid

Posted: Sun Dec 21, 2008 2:31 pm   Post subject: RE:float conversion logic

Well, I'm not on windows, and so don't have access the that calculator, even if it did work. The calculator that comes with mac is worse than the windows one.
OneOffDriveByPoster

Posted: Sun Dec 21, 2008 2:55 pm   Post subject: Re: RE:float conversion logic

Tyr_God_Of_War @ Fri Dec 19, 2008 9:03 pm wrote:
You know, the calculator that comes with windows can't do that. It just rounds. That ruins my cunning plan of using it to check your work. Maybe another, better calculator can...
Try

320CCCCCCCCCCCCC
/100000000000000

in decimal:

3606482581598293196 / 72057594037927936
= 50.049999999999999988897769753748 approx.

(This was done with the Windows calculator.)
Insectoid

Posted: Wed Dec 24, 2008 11:21 am   Post subject: RE:float conversion logic

So, I would have to divide the number by the base equivalent of 100? so to octal would be 5/144, and to binary would be 101/1100100. Right?
OneOffDriveByPoster

Posted: Wed Dec 24, 2008 12:03 pm   Post subject: Re: float conversion logic

Well 0.05 = 5/100.

You could do 1/20...

Converting from hex or oct to binary and vice versa is much simpler of course.

8 = 2^3 so an oct digit is 3 bits
16 = 2^4 so a hex digit is 4 bits

Here is the binary case.
 code: v   v            0.000011001 1100100  101 00000  <==           11 00100            1 111000            1 100100               10100000  <==                1100100
Insectoid

Posted: Wed Dec 24, 2008 12:08 pm   Post subject: RE:float conversion logic

I just though of something...

In base 10, you can multiply a decimal by 10 multiple times to make it >1. So, for hex, could I multiply by 16^length to make it >0, convert, then divide by 16^length again? Just a thought...

OneOffDriveByPoster

Posted: Wed Dec 24, 2008 12:23 pm   Post subject: Re: RE:float conversion logic

Exactly:

If you consider using multiplication, which the division process
implies when you "add zeros".

For each digit after the ones digit, you can multiply by the base to see
what the value of the digit is by taking the non-fractional part.

This is obvious using base-10.

 code: 0 . 0| 0.05 * 10 = 0.5 5| 0.5 * 10 = 5
0.05 = 0.05 (as expected)

 code: 0 . 0| 0.05 * 2 = 0.1 0| 0.1 * 2 = 0.2 0| 0.2 * 2 = 0.4  <== 0| 0.4 * 2 = 0.8 1| 0.8 * 2 = 1.6 1| 0.6 * 2 = 1.2 0| 0.2 * 2 = 0.4  <==
0.05 = 0.0000110011001100...
(This is what we got from long division too.)

 code: 0 . 0| 0.05 * 16 = 0.8 C| 0.8 * 16 = 12.8  <== C| 0.8 * 16 = 12.8  <==
0.05 = 0.0CCC...
(This is what we got from long division too.)
OneOffDriveByPoster

Posted: Wed Dec 24, 2008 12:28 pm   Post subject: Re: RE:float conversion logic

insectoid @ Wed Dec 24, 2008 12:08 pm wrote:
I just though of something...

In base 10, you can multiply a decimal by 10 multiple times to make it >1. So, for hex, could I multiply by 16^length to make it >0, convert, then divide by 16^length again? Just a thought...
You need to keep the remainder and deal with it. The multiplying process above is basically this.
Insectoid

Posted: Wed Dec 24, 2008 8:34 pm   Post subject: RE:float conversion logic

I got it! Multiply the number by target base^number of decimal places, divide by target base equivalent of 100 (64, 144, etc.), and then divide by base^number of decimal places. I think there's something missing, but I'm not sure...
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 11 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: