Re: OT How to represent a fraction in HEX?

From: Till Crueger (TillFC_at_gmx.net)
Date: 04/15/04


Date: Thu, 15 Apr 2004 10:24:24 +0200

On Wed, 14 Apr 2004 22:54:15 +0000, Steve wrote:

> I am having trouble finding the answer to this question, I'm thinking
> the solution must be blindingly obvious, so therefore of course I cannot
> see it.
>
> I wish to take a fraction in DEC say .4 and convert it to a HEX value. I
> also need to find a way to determine if a number is a fraction in HEX
> and convert it back to DEC.

[snip]
 
> I won't list them here cuz I want to solve the problems myself, I just
> happen to need to be pointed in the right direction for solving them, a
> formula or something would be nice.

There is a simple algorithm you can use for conversion of fractions. I can
give you the algorithm, so you can implement ist for yourself.

100 multiply the number by the base you are converting to
200 take the number greater than one, and use it as the
    next digit in your converted number
300 discard anything greater than one
400 repeat until satisfied or left with zero

Example:
.4 * 16 = 6.4 (result 0.6)
.4 * 16 = 6.4 (result 0.66)
...

Example 2:
0.21875 * 16 = 3.5 (result 0.3)
0.5 * 16 = 8 (result 0.38)
done

When you have figured out how fractions work, it should be fairly easy to
see how this algorithm works.
Till

-- 
Please add "Salt and Peper" to the subject line to bypass my spam filter


Relevant Pages

  • Re: struct ToString() not automatically invoked
    ... I have an implicit conversion from Fraction to decimal. ... WarehouseDetail warehouseInfo = stockItem + warehouse; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help me to understant sampling theory, decimation by integer factor
    ... Sometimes, to resample signal by rational factor, people use conversion ... There is no such thing as a fraction of a sample. ... Basically given a suitably sampled signal, you recover the ... original analog waveform and resample it at any fractional location ...
    (comp.dsp)
  • Re: Help me to understant sampling theory, decimation by integer factor
    ... Sometimes, to resample signal by rational factor, people use conversion ... There is no such thing as a fraction of a sample. ... original analog waveform and resample it at any fractional location ... was asking, "When we increase a sample rate, or decrease ...
    (comp.dsp)
  • Re: DecimalToFraction Algorithm
    ... >> I recently requested an alg to convert a decimal to fraction. ... >> the conversion should be accurate to 3 decimal places. ... > without adding a lot of conditional code to let it know what kind of ... > denominators you favor, how to choose between competing "favored" ...
    (comp.lang.basic.realbasic)
  • Re: struct ToString() not automatically invoked
    ... I have written a Fraction struct. ... I have an implicit conversion from Fraction to decimal. ... the compiler invokes my implicit conversion ...
    (microsoft.public.dotnet.languages.csharp)

Loading