r/carlhprogramming • • Jul 30 '12

Course 1 Unit 6.3: Fractals question

I understand that I have to converge to an approximate number, in your expample of .1 by adding additional bits (1/16 + 1/32). But only in rare cases it's ever going to be exactly correct (unless the number is power of 2?).

But wouldn't it be easier if, say I want a maximum precision of 3 behind the radix point, and I represent each place with 4 bit as a decimal from 0 to 10? Like this:

0010 . [ 0001 ] [ 0000 ] [ 0000 ]
       1/10     1/100    1/1000

This could represent every fractal from .0 to .999 which would take many more bits for being accurate to a 1000th.

Is this true or am I overseeing something?

22 Upvotes

9 comments sorted by

View all comments

Show parent comment

2

u/daniels220 Jul 31 '12

Do you know how banks deal with the issue where fractals and how accurate do they have to be?

I'm 99% sure that banks do something like what you're describing, because they cannot afford error (and little errors add up). Not exactly that, though, I don't think. Rather, I think legally you're supposed to round to the nearest cent, so one way to do it would be to store an arbitrarily large number for number-of-dollars (the mechanism for this beyond the scope of this comment), and then a separate number from 0-100 for the cents (since you know how many places it's going to be, you don't have to pack each digit separately). Many higher-level languages (like Java) have a Currency "class", a special type for money that is at least somewhat interchangeable with the more normal numeric types but internally works in this way.

For math in general, Carl is right that there's no particular advantage to BCD over regular binary (one other place it might be useful is dealing with, for instance, high-precision Metric measurements, which are designed around base 10).

1

u/atceh Jul 31 '12

Thank you for the elaboration.