-----------------------------------
Insectoid
Fri Dec 19, 2008 12:04 pm
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:
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(".")
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
Fri Dec 19, 2008 12:46 pm
Re: float conversion logic
-----------------------------------
50.05 in hex = ?
50 in hex: 32
.05 in hex = ?
5 / 100
5h / 64h
Long division
0.0CCC...
64 5 00
500
You are looking for 32.0CCC...
-----------------------------------
Tyr_God_Of_War
Fri Dec 19, 2008 9:03 pm
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
Sun Dec 21, 2008 2:31 pm
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
Sun Dec 21, 2008 2:55 pm
Re: 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...Try
320CCCCCCCCCCCCC
/100000000000000
in decimal:
3606482581598293196 / 72057594037927936
= 50.049999999999999988897769753748 approx.
(This was done with the Windows calculator.)
-----------------------------------
Insectoid
Wed Dec 24, 2008 11:21 am
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
Wed Dec 24, 2008 12:03 pm
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.
v v
0.000011001
1100100 101 00000 0, convert, then divide by 16^length again? Just a thought...
-----------------------------------
OneOffDriveByPoster
Wed Dec 24, 2008 12:23 pm
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.
0
.
0| 0.05 * 10 = 0.5
5| 0.5 * 10 = 50.05 = 0.05 (as expected)
0
.
0| 0.05 * 2 = 0.1
0| 0.1 * 2 = 0.2
0| 0.2 * 2 = 0.4