Recent

Author Topic: High precision float?  (Read 1978 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: 5737
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?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Handoko

  • Hero Member
  • *****
  • Posts: 3053
  • 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: 836
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 »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

mischi

  • Full Member
  • ***
  • Posts: 117
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: 8039
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 »
Hamlet 1.4 (nothing wrong with the Danish, btw)

Mr.Madguy

  • Sr. Member
  • ****
  • Posts: 440
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.
Many major changes - Version has been changed to 3.0!
Crazy unit testing
Lack of multiple inheritance turns it into abomination.

Thaddy

  • Hero Member
  • *****
  • Posts: 8039
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.
Hamlet 1.4 (nothing wrong with the Danish, btw)

VTwin

  • Hero Member
  • *****
  • Posts: 665
  • 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

macOS 10.11.6: Lazarus 2.1.0 svn 61357M (64 bit Cocoa trunk)
Ubuntu 18.04.2: Lazarus 2.0.0 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.0 (64 bit on VBox)

mtrsoft

  • New member
  • *
  • Posts: 24
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: 5737
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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Thaddy

  • Hero Member
  • *****
  • Posts: 8039
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 »
Hamlet 1.4 (nothing wrong with the Danish, btw)

440bx

  • Hero Member
  • *****
  • Posts: 836
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.)


using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

garlar27

  • Hero Member
  • *****
  • Posts: 594
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?