Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 float conversion logic
Index -> Programming, Ruby -> Ruby Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: 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:
Ruby:

require 'jcode'

class NumberConverter
        attr_reader :decimal
        def initialize (number, base)
                @number = number
                @base = base
                @integer = @number.to_i #the integer portion of the float
                @decimal = (@number.to_s.split(".")[1]).reverse.to_i #the decimal portion of the float, flipped
        end
       
        def to_base(base)
                chars =("0".."9").to_a+("A".."Z").to_a
                answer = ""
                temp = @integer
                until (temp == 0) do
                        answer = chars[(temp %base)] + answer #adds to front of answer
                        temp /= base
                end
                temp = @decimal
                answer += "."
                until (temp == 0) do
                        answer += chars[(temp %base)] #adds to back of answer
                        temp /= base
                end
                answer.empty? ?  "0" : answer
        end
end

puts (NumberConverter.new(50.05 s, 10).to_base(16))


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).
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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...
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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




PostPosted: 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:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: