Recent

Author Topic: High precision float?  (Read 6511 times)

SWire

  • Newbie
  • Posts: 2
High precision float?
« 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 :)

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: High precision float?
« Reply #1 on: January 11, 2019, 06:12:35 pm »
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?

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: High precision float?
« Reply #2 on: January 11, 2019, 06:17:24 pm »

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: High precision float?
« Reply #3 on: January 11, 2019, 06:29:08 pm »
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.)
« Last Edit: January 11, 2019, 06:31:07 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

mischi

  • Full Member
  • ***
  • Posts: 170
Re: High precision float?
« Reply #4 on: January 11, 2019, 06:45:47 pm »
Isn't the extra precision of 80 bit of extended limited to 32bit systems, whereas it falls back to 64bit on other systems?

SWire

  • Newbie
  • Posts: 2
Re: High precision float?
« Reply #5 on: January 11, 2019, 06:56:32 pm »
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.
« Last Edit: January 11, 2019, 07:03:01 pm by SWire »

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: High precision float?
« Reply #6 on: January 11, 2019, 07:01:42 pm »
@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.
« Last Edit: January 11, 2019, 07:09:30 pm by Thaddy »
Specialize a type, not a var.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: High precision float?
« Reply #7 on: January 11, 2019, 07:07:50 pm »
If you encounter such problems, they you should learn about 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.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: High precision float?
« Reply #8 on: January 11, 2019, 07:10:59 pm »
If you encounter such problems, they you should learn about 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.
Specialize a type, not a var.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: High precision float?
« Reply #9 on: January 11, 2019, 07:17:28 pm »
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
« Last Edit: January 11, 2019, 07:23:02 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

mtrsoft

  • New Member
  • *
  • Posts: 44
Re: High precision float?
« Reply #10 on: January 11, 2019, 07:33:31 pm »
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.

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: High precision float?
« Reply #11 on: January 11, 2019, 07:36:09 pm »
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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: High precision float?
« Reply #12 on: January 11, 2019, 07:42:32 pm »
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.
« Last Edit: January 11, 2019, 07:45:24 pm by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: High precision float?
« Reply #13 on: January 11, 2019, 07:52:45 pm »
@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.)


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: High precision float?
« Reply #14 on: January 11, 2019, 11:33:49 pm »
Just Curious:

Is easy to work with BCD numbers? Can be used to floating point operations?

 

TinyPortal © 2005-2018