06-13-2017 01:51 PM
Hello,
I'm a bit new to LabVIEW and am currently starting by doing some Project Euler to try to learn logic and ways to think about data float in LabVIEW. One problem I'm working on requires me to find 100!, which is equal to
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
However, when I try to find this in LabVIEW with a for loop and using extended floating representation I am returned with 9.3326215443944153E+157. If I view this as non-scientific then it is just followed by zeros and not the correct value. Is there a way to get more digits on large numbers like this in LabVIEW?
Solved! Go to Solution.
06-13-2017 02:17 PM
The floating point processors on most computers are only guaranteed to be accurate to the limit of a 64-bit float.
Greater precision can be achieved by using software instead of hardware to do the arithmetic.
If you hunt around, you might be able to find some very high precision arithmetic software.
Or you could try to write your own (not a small task).
06-13-2017 02:21 PM
Paul nailed it.
The extended precision floating point uses 80 bits but if you look at the arbitrary precision arithmetic on Wikipedia you will see you need 128 bits for 34!
That article does include some code that you could translate into LV if you are up to it.
Ben
06-13-2017 02:23 PM
Hi TheStrangeQuark,
There is an input on top of the Format Into String function that specifies the format of the string. This will allow you to change the conversion between your floating point number and the output string. The String formatting codes will be helpful here.
However, I suspect the lack of precision that is frustrating you is simply due to the data type you are using and the limitations of LabVIEW. There is a finite limit to the amount of precision you can achieve with a 128-bit floating point number. I can't say for certain simply because I am not that familiar with 128-bit representations (that's REALLY big), but it is possible that you are butting up against this limit. If that is the case, you are welcome to research this topic further, but you will probably not learn anything particularly useful. You could start by researching the way that LabVIEW represents numbers this large on your particular OS at the bit level (it will vary across OS's).
However, if what you would like to do is learn about using LabVIEW in practical applications, your time would probably be better spent elsewhere.
Here's a great place to start!
Best,
Duncan W.
06-13-2017 03:28 PM
We had a LabVIEW coding challenge here many years ago to find all digits of large factorials. (My code found the ~36000 digits of 10000! In milliseconds).
Sorry, posting by phone, but try a search...
06-13-2017 09:40 PM
@Ben wrote:
Paul nailed it.
The extended precision floating point uses 80 bits but if you look at the arbitrary precision arithmetic on Wikipedia you will see you need 128 bits for 34!
That article does include some code that you could translate into LV if you are up to it.
Ben
Note that the extra 16 bits in 80-bit floating point are guard bits so the result will be completely accurate in 64-bit format.
06-14-2017 06:16 AM
I tried to search for this challenge and found the page no longer exists and also does not exist in the archive of the old challenges.
06-14-2017 06:57 AM
i tried to search for it too, but even the waybackmachine has no save for the result page of the 'large number challenge'.
so, @altenbach, if you could find it (at your convenience) that would be very nice
regards
06-14-2017 10:48 AM
Yes, I still have the code, but it pulls way too many tricks to speed up the calculation (Packing, FFT, in-placeness, speed-formatting) and none of it is necessary for your problem.
To get you started, think of the numbers as an array of single digits numerics (simplest is in based 10, least significant digit first), then a multiplication is just a convolution followed by fixing the carry to keep the digits in range. same as when you learned multiplication in primary school. 😄
06-14-2017 03:35 PM - edited 06-15-2017 09:38 AM
@altenbach wrote:
To get you started, think of the numbers as an array of single digits numerics (simplest is in based 10, least significant digit first), then a multiplication is just a convolution followed by fixing the carry to keep the digits in range. same as when you learned multiplication in primary school. 😄
Here's a very (very!) primitive implementation of what I just said. (Huge improvements are possible, of course). Make sure to study it and try to understand every single part of the code!
(The number of digits for 10000! is still listed incorrectly on the top google search result for "10000 factorial" 35659 digits (wrong!) instead of 35660 (correct!) digits. A better place to verify the result is here, for example.)
If you really want to speed things up to be able to deal with much larger numbers, have a look here.