Computer Science Canada float conversion logic |
Author: | Insectoid [ 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). |
Author: | OneOffDriveByPoster [ 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
You are looking for 32.0CCC... |
Author: | Tyr_God_Of_War [ 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... |
Author: | Insectoid [ 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. |
Author: | OneOffDriveByPoster [ 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.) |
Author: | Insectoid [ 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? |
Author: | OneOffDriveByPoster [ 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.
|
Author: | Insectoid [ 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... |
Author: | OneOffDriveByPoster [ 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.
(This is what we got from long division too.)
(This is what we got from long division too.) |
Author: | OneOffDriveByPoster [ 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...
You need to keep the remainder and deal with it. The multiplying process above is basically this.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... |
Author: | Insectoid [ 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... |