edwig / BCD

Binary Coded Decimal. Research project for use with SQL_NUMERIC_STRUCT datatype
MIT License
12 stars 1 forks source link

Library does not work with more than 38 digits. #10

Open blackjetrock opened 10 months ago

blackjetrock commented 10 months ago

I would like to use this library with 200 digits, 100 before the decimal point and 100 after. That would be equivalent to 1E99 to 1E-99 and allows me to use it for calculators with that range of numbers. At the moment it doesn't work above 38 digits, and there is a comment to that effect in the code. Is it known what the reason for the 38 digit limit is?

edwig commented 10 months ago

Have a look at bcd.h at lines 52-56. There are two constants that control this behaviour: bcdDigits : The number of digits per integer (here 8) bcdLength: The number of integers in the mantissa (here 5) Currently the values are so that the maximum (unrounded) number of mantissa = 8 5 = 40 digits (38 rounded) To increase the mantissa considder to increase the bcdLenght* to more integers. For 200 that would be 200/8 = 25. And then you should be able to use mantissa's of 200 digits.

blackjetrock commented 10 months ago

Hello,

I have done this and unfortunately the code crashes. I have ported the code to the RP Pico and above 38 digits I get an error that I don't understand yet. I also expected it to work for any number of digits. The comment that mentions a rash is actually one that I put in the code. the problem is in DividePositive but isn't a divide by zero. I think it's some form of overflow of the digits outside the mdata array bounds. Maybe.

edwig commented 10 months ago

Hello blackjetrock,

The proof of the pudding is in the eating. So….. I checked in an extra class “bcd200” with a 200 digits mantissa. Pull the latest from https://github.com/edwig/bcd The unit tests of bcd are copied to UnitTestsBCD200.

Besides the bcdLength the following changes were made

All unit tests of bcd200 run in the green!

Have fun!