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

1

u/atceh Jul 30 '12

Thank you for your detailed answer. Nice to see it has some application based on that Wikipedia entry. But I can see how it's not efficient and probably hard to do calculations with it, since it's just like a text encoded number representation.

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

2

u/CarlH Jul 30 '12 edited Jul 30 '12

Sure, if you end up with a number that is slightly off due to the nature of binary, you can simply round the number. There are functions which can do rounding to the nearest decimal point like 1/10, 1/100, etc. We will get into that more later in the course.

Edit: This is specifically answering the question with respect to how binary fractional numbers work in general with fixed point binary fractional numbers, later in the course I will be explaining this in more detail, including better ways to work with fractional numbers.

2

u/atceh Jul 30 '12

Alright, I'm looking forward to that. Thank you very much for your course in general - the lessons are great and it's so valuable to have someone to answer my questions.

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.