Lazarus
Programming => General => Topic started by: SWire on January 11, 2019, 06:03:44 pm
-
I have encountered a problem. I need more precision in my float calculations.
How do I make that happen?
The calculations arent overly complicated and I dont care about how performance efficent it is.
So a "quik and dirty" approach would do.
(Im a complete novice, so idiot level instructions would be nice).
Thanks in advance :)
-
I need more precision in my float calculations.
This is hard to believe. What exactly do you do which makes you think that the precision of the standard floating point types is not enough?
-
@SWire
Have you tried Extended?
http://wiki.freepascal.org/Variables_and_Data_Types
Or maybe:
http://forum.lazarus.freepascal.org/index.php?topic=15895.0
http://delphiforfun.org/programs/library/BigFloat.htm
I wonder what calculation you're going to do. Can you tell us?
-
I need more precision in my float calculations.
How do I make that happen?
The calculations arent overly complicated and I dont care about how performance efficent it is.
So a "quik and dirty" approach would do.
Thanks in advance :)
You'll get more precision using the types "double" and/or "extended". "extended" has the most significant digits you can get.
Declare your variables as "extended". That should do it (at least it does in most cases.)
-
Isn't the extra precision of 80 bit of extended limited to 32bit systems, whereas it falls back to 64bit on other systems?
-
Unfortunatly double isnt enough. For some reason extended doesnt seem to improve the situation more than double.
The calculations are some rather simple mathematical series.
Althoug I need to run them around 100 times, the errors add up.
-
@440bx : A real extended is 80 bit default on 32 bit, but only 64 bit on x86_64, but:
That limitation is technically only for window x86_64 due to Win64 ABI limitations on win64. E.g. Linux x86_64 can be compiled to use the fpu unit and so can ( not default) support 80 bit. You need to compile the compiler yourself to have that feature. Anyway really high precision (much better than 80 bit) is available in software at an additional speed cost. And the x87 FPU feature on x86_64 is also kind of slow.
Whichever way you look at it precision comes at a price.
Florian explained how to do this (again) very recently on the fpc-devel mailing list.
-
If you encounter such problems, they you should learn about computational mathematics (https://en.wikipedia.org/wiki/Computational_mathematics). Problem is: usual math doesn't work on computers, as it's assumed, that precision is infinite in usual math, but in reality precision is finite, so you should learn about calculation errors and how they accumulate with certain operations. As I remember, absolute errors accumulate, when using addition, and relative errors accumulate, when using multiplication. When error exceeds your precision, your result becomes uncertain. So you should use special computation methods, not just arbitrary computing from usual math.
-
If you encounter such problems, they you should learn about computational mathematics (https://en.wikipedia.org/wiki/Computational_mathematics).
Not necessary: there are several big number libraries in the standard distribution. But indeed it would be good to know if you know why.
-
Unfortunatly double isnt enough. For some reason extended doesnt seem to improve the situation more than double.
The calculations are some rather simple mathematical series.
Althoug I need to run them around 100 times, the errors add up.
Can you explain further, is it really the number of digits, or the rounding error that is the problem. Perhaps the rounding error can be minimized.
If really necessary you could try:
https://gmplib.org/
Apparently Pascal headers are available for Free Pascal:
https://github.com/VadimAnIsaev/GNU-MP-for-FreePascal
I have not tried these.
EDIT: As Thaddy notes there are other options. See this thread as well:
http://forum.lazarus.freepascal.org/index.php?topic=22016.0
-
Check out DAMath and AMath at
http://www.wolfgang-ehrhardt.de/misc_en.html#amath
FPC /Lazarus' Math unit is based on this, but only uses a very limited amount of the available functionality.
-
Unfortunatly double isnt enough. For some reason extended doesnt seem to improve the situation more than double.
The calculations are some rather simple mathematical series.
Althoug I need to run them around 100 times, the errors add up.
Neil Armstrong and his crew would be flying somewhere in our solar system instead of having landed on the moon if standard floating point types would not be accurate enough.
Read a book on numerical methods, like "Numerical recipes" by Press et al. They demonstrate the elemental errors on the first pages.
-
Unfortunatly double isnt enough. For some reason extended doesnt seem to improve the situation more than double.
The calculations are some rather simple mathematical series.
Althoug I need to run them around 100 times, the errors add up.
Neil Armstrong and his crew would be flying somewhere in our solar system instead of having landed on the moon if standard floating point types would not be accurate enough.
Read a book on numerical methods, like "Numerical recipes" by Press et al. They demonstrate the elemental errors on the first pages.
@wp that's a contradiction in terms. I know that book. It merely demonstrates that accuracy is relative. In this case they were close enough to earth and close enough to the moon.
Although it also demonstrates - later - that accuracy comes at a price: time or distance. And series math. Approximations are the easy way to get accurate.... O:-)
First edition was Pascal I believe.
-
@440bx : A real extended is 80 bit default on 32 bit, but only 64 bit on x86_64, but:
That limitation is technically only for window x86_64 due to Win64 ABI limitations on win64. E.g. Linux x86_64 can be compiled to use the fpu unit and so can ( not default) support 80 bit. You need to compile the compiler yourself to have that feature. Anyway really high precision (much better than 80 bit) is available in software at an additional speed cost. And the x87 FPU feature on x86_64 is also kind of slow.
Whichever way you look at it precision comes at a price.
Florian explained how to do this (again) very recently on the fpc-devel mailing list.
The point, which you obviously missed, is that "extended" will provide the most precision (at least on Intel processors.)
As far as the rest, extended can sometimes be a synonym for double but, that doesn't change the fact that it is the type that will provide the greatest precision available short of coding your own floating point routines, in which case, you can get as precise as you want (or your programming abilities enable you to get - in your case, I would suggest you stick with integers.)
-
Just Curious:
Is easy to work with BCD numbers? Can be used to floating point operations?
-
if you can live with a 4 digit fraction then try the Currency Type.
-
Just Curious:
Is easy to work with BCD numbers? Can be used to floating point operations?
Very easy and yes, can be used for floating point operation. Any-point operation, in fact, with the appropiate design. :)
-
[…]
If really necessary you could try:
https://gmplib.org/
Apparently Pascal headers are available for Free Pascal:
https://github.com/VadimAnIsaev/GNU-MP-for-FreePascal
I have not tried these.
[…]
“Try”? Just use. GMP works fine (http://wiki.freepascal.org/gmp). However, I wanna note the headers – not the ones you referenced – are available as part of the package fp-units-math. They (https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/gmp/src/gmp.pas?view=log) might be dated, but still I prefer work that's done at one single place. Therefore I don't know what to think of Vadim's work.
-
@440bx : A real extended is 80 bit default on 32 bit, but only 64 bit on x86_64, but:
That limitation is technically only for window x86_64 due to Win64 ABI limitations on win64. E.g. Linux x86_64 can be compiled to use the fpu unit and so can ( not default) support 80 bit. You need to compile the compiler yourself to have that feature. Anyway really high precision (much better than 80 bit) is available in software at an additional speed cost. And the x87 FPU feature on x86_64 is also kind of slow.
Not quite correct: all x86_64 targets except Win64 have Extended enabled by default. Only for Win64 one must enable it by recompiling the compiler with FPC_SUPPORT_X87_TYPES_ON_WIN64 (see compiler/x86_64/symcpu.pas, line 181 and compiler/psystem.pas, line 314) - of course the remainder of the FPC distribution needs to be compiled then as well.
-
A clearer explanation on your part would help, you haven't exposed any real problems and we don't have any example code to analyze.... Please, expose the details to the forum, the interventions will be more useful.
Unfortunatly double isnt enough. For some reason extended doesnt seem to improve the situation more than double.
The calculations are some rather simple mathematical series.
Althoug I need to run them around 100 times, the errors add up.
-
here's another arbitrary precision floating point math library, MParith http://www.wolfgang-ehrhardt.de/mp_intro.html
The MPArith package contains Pascal/Delphi source for multi precision integer, rational, real and complex floating point arithmetic and functions