Page 1 of 1

Choosing variable types for a financial application

Posted: Sun Oct 02, 2022 10:41 am
by Oso
I'm trying to do a refresher on my knowledge of variable types, which I think is long overdue because I last studied this between 1982 - 1984. Shortly after that, I began working under an environment in which everything is non-typed :D

Just to clarify, where the documentation says "unlimited", presumably there is nevertheless a limit since the number of bytes used for storage is fixed? The page says "see below" but I'm not sure what it refers to.

Float .f 4 bytes unlimited (see below)
Quad .q 8 bytes -9223372036854775808 to +9223372036854775807
Double .d 8 bytes unlimited (see below)

https://www.purebasic.com/documentation ... ables.html

Re: Choosing variable types for a financial application

Posted: Sun Oct 02, 2022 10:49 am
by Little John
Oso wrote: Sun Oct 02, 2022 10:41 am Just to clarify, where the documentation says "unlimited", presumably there is nevertheless a limit since the number of bytes used for storage is fixed? The page says "see below" but I'm not sure what it refers to.

Float .f 4 bytes unlimited (see below)
Quad .q 8 bytes -9223372036854775808 to +9223372036854775807
Double .d 8 bytes unlimited (see below)

https://www.purebasic.com/documentation ... ables.html
Yes, the number of bytes used for storage is fixed for all numeric variables. The number of bytes is not fixed for strings, arrays, lists, and maps – but there is alwas a limit anyway, e.g. the amount of installed RAM.
For a financial application, do NOT use .f or .d, but only whole numbers, e.g. .q. Otherwise you'll encounter a loss of precision, which is not fun in a financial apploication.

Re: Choosing variable types for a financial application

Posted: Sun Oct 02, 2022 11:19 am
by Olli
Hello Oso,

for a financial application, you should use the quad and handle the cents with these integers.

The range is +/- 2 exacents.

For ratio, between 2 values, you should use quad again.

i.e : 001.01567 (EUR/USD)
Consider 101567 in a quad, and add the floating point only in the display string. Like that, it will ever be a 10^x division which will close your ops, and, in these ways, the rests are calculated with a 10^x modulo. These rests are distributed depending several laws and commercial rules.

Re: Choosing variable types for a financial application

Posted: Sun Oct 02, 2022 12:02 pm
by Oso
Many thanks Little John and Olli, great starting point for me. Yes, understood about not using floating point.

I presume I can mix types in calculations where it isn't an exact monetary amount, like...

Code: Select all

Rate.f = currency1.q / currency2.q

Re: Choosing variable types for a financial application

Posted: Sun Oct 02, 2022 12:54 pm
by juergenkulow

Re: Choosing variable types for a financial application

Posted: Sun Oct 02, 2022 9:30 pm
by AZJIO
BigInt module (SSE2)
I don't know if this might be what you are looking for.

Re: Choosing variable types for a financial application

Posted: Mon Oct 03, 2022 1:10 am
by Olli
Oso wrote:I presume I can mix types in calculations where it isn't an exact monetary amount, like...
No, you cannot do it. For example, 0.0001 on forex it is $0.05 on a minimum order. If you execute a hft, you won't be happy when the hysteresis created by a small error of 0.0001 will pump 50 mega$ per seconds. Low tide...

Even the double precision floating points are for the garbage, because the ratios, you think on start it is just to see, but, not.

A ratio, for 4 decimals, it is 12345, a strong integer. If you want to display it, you use Str(12345) and you insert a dot in the displayed string or you use plot, or displaySprite().

To get it from two values,

Code: Select all

ratio = a * 100000
rest = ratio % b
ratio / b
And you could even see the rest is useful.

I also bet nobody on this forum is able to display a amortization schedule with floating point numbers (single as double).

Don't forget also the price of petroleum was negative, 30 months ago : you could be payed to buy it, if you were able to buy it.

The precision of a floating point value, it is a joke. You can also try to build a string number system. It is slower, but it is a very good experience : to teach a computer to count.

Re: Choosing variable types for a financial application

Posted: Mon Oct 03, 2022 1:11 am
by StarBootics
Another alternative to deal with big numbers : VogelsNumberFormat - OOP

Best regards
StarBootics

Re: Choosing variable types for a financial application

Posted: Mon Oct 03, 2022 10:20 am
by captain_skank
I gave up on this in PB and do it all on the MySQL/MariaDB backend - saved me end of trouble, especially when dumping in to Excel.

Re: Choosing variable types for a financial application

Posted: Tue Oct 04, 2022 7:47 am
by Bisonte
to handle money values i used quads to avoid rounding problems.
So I don`t use values like : 12,56 , I use 1256.

And I thought, that the total money value of one person is "floating" a quad variable ;)