SQLite Forum

decimal_div
Login

decimal_div

(1) By Tom B (tb13) on 2020-08-14 13:58:19 [link] [source]

Hi, the new decimal extension is great, thx for adding it.  Curious, why is there no decimal_div function?  Would be nice for finding averages.

select 1.1 + 2.2 = 3.3;
select decimal_add(1.1, 2.2) = decimal(3.3);

 Second result row is 1, super!

(2) By Richard Hipp (drh) on 2020-08-14 14:24:21 in reply to 1 [source]

why is there no decimal_div function?

  1. Addition, subtraction, and multiplication give you an answer with finite precision. Division does not (at least not in the case where the denominator contains prime factors other than 2 and 5). So it is difficult to know to what precision the division should be computed.

  2. Division is more difficult to implement.

  3. Division is a less-frequently needed operation.

All that said, if you have sample code to implement decimal_div(), I will be happy to look at it. :-)

(3) By Tom B (tb13) on 2020-08-14 14:55:13 in reply to 2 [link] [source]

I did not know that about the finite precision.  I don't happen to have any sample code for division (at least, not on me ;), so can't help in that regard.  Decimal math code looked pretty intense, last time I checked.

Thanks for adding this extension Dr. Hipp!
Tom

(4) By Larry Brasfield (LarryBrasfield) on 2020-08-14 15:04:29 in reply to 1 [link] [source]

As Richard says, division would be problematic.

Mathematically, the set of decimal numbers is not closed under division. Unlike multiply, add and subtract, division of decimal numbers may or may not (and more likely not) produce a decimal number. What would you want then?

To get division while more or less honoring the spirit of the decimal type, you can write decimal_mul( whateverDecimalValue, 1.0/divisor ) . You will have to accept that this may not be mathematically exact (just as you would have usually had to accept if decimal_div() existed.)