Re: bignum with floating point
- From: Thad Smith <ThadSmith@xxxxxxx>
- Date: Wed, 25 Apr 2007 22:22:36 -0600
banansol@xxxxxxxxxxxx wrote:
I want to program a bignum library in C just for learning,
it doesn't seem too hard (I don't care about speed) when
considering just integer numbers, but when I add support
for division and need floating point numbers I doesn't
look so easy anymore. I wonder if it's hard to do this.
Is there some easy trick to do this? Can I just round
off some fixed digits to the right of the decimal-point?
Or do I need to get a deep understanding of
floating point representations and IEEE standards?
You don't need to understand IEEE standards, but it helps to understand the fundamentals of floating point. Instead of integers, think of fixed-point values with the binary point to the left of the most-significant bit: 0.xxxxx, where xxxxx is an arbitrary binary value to any desired length. Select an exponent base, add a sign bit and multiply by a power of base and you have a floating point representation:
-1**s * 0.xxxxxx... * base**e.
For IEEE base = 2, but other bases are possible. Older-design IBM computers used a base of 16. You might want to use a base of 256 to minimize bit shifting at the expense of a few wasted bits (less important for longer formats).
If the leading base digit in the mantissa is non-zero, the representation is normalized: 0.1xxxx... for base = 2.
Normally, floating point operations start with normalized values. Multiplication and division are straight-forward. Addition and subtraction require that you first shift an operand to align the base point of the two operands. If you use base = 256 on an 8-bit unsigned char machine, you can get the same results by selective indexing, rather than actual shifts. After the addition or subtraction, the results should be normalized.
--
Thad
.
- References:
- bignum with floating point
- From: banansol
- bignum with floating point
- Prev by Date: Re: random integer
- Next by Date: Re: Change for a Dollar
- Previous by thread: Re: bignum with floating point
- Next by thread: Change for a Dollar
- Index(es):
Relevant Pages
|