If I debug this:
Code: Select all
Debug 538.0 * 0.0254
I really appreciate any help
Code: Select all
Debug 538.0 * 0.0254
Code: Select all
Debug StrD(538.0 * 0.0254) ; Outputs 13.6652
Hi eNano, and welcome to PureBasic!eNano wrote: Fri Aug 30, 2024 12:20 amwhy the result is 13.6652000000000004575895218 when after the 4th decimal all should be zero?
Code: Select all
Debug ValD("123.456") ; 123.4560000000000030695446185
Debug ValF("123.456") ; 123.45600128173828
Code: Select all
Debug StrD(123.456, 2) ; 123.46
Debug StrF(123.456, 2) ; 123.46
It depends on the precision that you need and the range of values and the operations you use.eNano wrote: Fri Aug 30, 2024 4:44 pm Thanks for all the answers! I'm working with some imperial to metric unit conversions in purebasic, I was guessing this was some "binary kind of thing" that I don't understand. Is it ok if I keep working with float numbers and trust the results? will I loose too much accuracy if I do a couple of conversions between units? I just want to know if I'm doing this correctly. Thanks a lot
Code: Select all
; Original value after some mathematical operation:
Define total.d = 54.9551111111111
total = total * 1000
Define total_rounded.d = Round(total * 0.1, #PB_Round_Up) * 0.01
; When need to show it:
Debug FormatNumber( total_rounded,2,".",",") ; result = 54.96
Code: Select all
Debug Int(0.7 + 0.2 + 0.1)
Nah, you can just use whole integers for the currency (to include the cents without floats) and then just insert a decimal point when needed for display. Lots of financial apps do that (I've even used them in government apps before). So for $100.21 + 3 cents, you'd do 10021+3 (to get 10024) but your app just displays it as "100.24" because the last two digits are always the cents.Fred wrote: Fri Aug 30, 2024 8:21 pmIf you need money like operation were all decimals needs to be perfect, double or float won't be enough. You need some special decimal libs like libmpec or use a language which has builtin support for decimals like C#
Code: Select all
; Adding 3 cents to $100.21
Debug 100.21+0.03 ; 100.2399999999999948840923025
Debug 10021+003 ; 10024
@BarryG: That's a fairly good approach, but it would still raise problems with multipliers and divisors, where the mantissa results would not always conform to the required decimal places. A simple loan scenario would entail amortisations and prorations, and would easily break this method.BarryG wrote: Sat Aug 31, 2024 3:58 pm...you can just use whole integers for the currency (to include the cents without floats) and then just insert a decimal point when needed for display. ...
If things were only that simple, BarryBarryG wrote: Sat Aug 31, 2024 3:58 pmNah, you can just use whole integers for the currency (to include the cents without floats) and then just insert a decimal point when needed for display.Fred wrote: Fri Aug 30, 2024 8:21 pmIf you need money like operation were all decimals needs to be perfect, double or float won't be enough. You need some special decimal libs like libmpec or use a language which has builtin support for decimals like C#
Code: Select all
PACKQTY=12 ; 12 in a pack
PACKPRICE=659 ; Price per pack $6.59
UNITPRICE.f = PACKPRICE / PACKQTY ; Price per unit
Code: Select all
PACKPRICE=6590000 ; Price per pack $65,900.00