LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

More significant digits on large numbers

Solved!
Go to solution

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?

0 Kudos
Message 1 of 20
(6,486 Views)

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).

"If you weren't supposed to push it, it wouldn't be a button."
Message 2 of 20
(6,468 Views)

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 3 of 20
(6,461 Views)

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.

0 Kudos
Message 4 of 20
(6,459 Views)

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...

Message 5 of 20
(6,441 Views)

@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.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 6 of 20
(6,411 Views)

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.

0 Kudos
Message 7 of 20
(6,392 Views)

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


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 8 of 20
(6,387 Views)

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. 😄

0 Kudos
Message 9 of 20
(6,371 Views)
Solution
Accepted by TheStrangeQuark

@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.

 

 

 

Download All
Message 10 of 20
(6,344 Views)