[Solved] Inaccuracy for big power function

Discuss the spreadsheet application
Post Reply
Lu5ck
Posts: 1
Joined: Mon Mar 04, 2013 10:57 am

[Solved] Inaccuracy for big power function

Post by Lu5ck »

There seems to be inaccuracy when the numbers become too big, more than 15 digits. For example, 12^18 will gives 26623333280885200000 which is of course inaccurate.

Is there any ways to make this calculation accurate?
Last edited by MrProgrammer on Tue Feb 22, 2022 5:54 am, edited 1 time in total.
Reason: Tagged ✓ [Solved] since the calculation could be done with multiprecision arithmetic, as demonstrated -- MrProgrammer, forum moderator
Openoffice 3.4.1
Windows 7 x64 SP1
User avatar
Hagar Delest
Moderator
Posts: 32665
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Inaccuracy for big power function

Post by Hagar Delest »

Hi and welcome to the forum!

Spreadsheets are not designed for such calculation I think. Use a dedicated software.
It may be related to the floating point calculation method used in computers.

Please add '[Solved]' at the beginning of your first post title (edit button) if your issue has been fixed.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Inaccuracy for big power function

Post by RoryOF »

You might find useful information on spreadsheet accuracy in this paper
http://www.jstatsoft.org/v34/i04/paper
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
jrkrideau
Volunteer
Posts: 3816
Joined: Sun Dec 30, 2007 10:00 pm
Location: Kingston Ontario Canada

Re: Inaccuracy for big power function

Post by jrkrideau »

RoryOF wrote:You might find useful information on spreadsheet accuracy in this paper
http://www.jstatsoft.org/v34/i04/paper
Thanks Rory, I had not seen that one.
LibreOffice 7.3.7. 2; Ubuntu 22.04
jrkrideau
Volunteer
Posts: 3816
Joined: Sun Dec 30, 2007 10:00 pm
Location: Kingston Ontario Canada

Re: Inaccuracy for big power function

Post by jrkrideau »

Lu5ck wrote:There seems to be inaccuracy when the numbers become too big, more than 15 digits. For example, 12^18 will gives 26623333280885200000 which is of course inaccurate.

Is there any ways to make this calculation accurate?
Not with a spreadsheet. Below is an extract from the R FAQ . The issues apply to a spreadsheet such as AOO Calc as well. For more information than you are ever likely to want to know follow up the Goldberg paper linked in the extract.

I believe that Mathematica or Maple will handle arbitrarily large numbers. I think that there also is some OpenSource software that will slso do so but I don't have a link.

Code: Select all

7.31 Why doesn't R think these numbers are equal?

The only numbers that can be represented exactly in R's numeric type are integers and fractions whose denominator is a power of 2. Other numbers have to be rounded to (typically) 53 binary digits accuracy. As a result, two floating point numbers will not reliably be equal unless they have been computed by the same algorithm, and not always even then. For example

     R> a <- sqrt(2)
     R> a * a == 2
     [1] FALSE
     R> a * a - 2
     [1] 4.440892e-16

The function all.equal() compares two objects using a numeric tolerance of .Machine$double.eps ^ 0.5. If you want much greater accuracy than this you will need to consider error propagation carefully.

For more information, see e.g. David Goldberg (1991), “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys, 23/1, 5–48, also available via http://www.validlab.com/goldberg/paper.pdf. 
LibreOffice 7.3.7. 2; Ubuntu 22.04
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Inaccuracy for big power function

Post by acknak »

jrkrideau wrote:... I think that there also is some OpenSource software that will slso do so but I don't have a link. ...
Emacs "calc" mode--for one:
12
18
^ 26623333280885243904
12
38
^ 102067469997853225734913580209377959215104
I'm no mathematician, but it seems a little unfair to compare sqrt(2)^2 to 2: isn't that impossible to compute with a finite calculator?
AOO4/LO5 • Linux • Fedora 23
User avatar
MrProgrammer
Moderator
Posts: 4907
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Inaccuracy for big power function

Post by MrProgrammer »

acknak wrote:I'm no mathematician, but it seems a little unfair to compare sqrt(2)^2 to 2: isn't that impossible to compute with a finite calculator?
All modern floating point systems store numbers as rational fractions where the denominator is a power of 2. It has been known for at least 2500 years that √2 is not equal to any rational fraction, that is, it's an irrational number (see below). So √2 certainly can't be represented as one where the denominator is a power of 2, no matter how many bits we are allowed to use.

Unfortunately for the OP, puny Windoze has no built-in tools for high precision calculations like Unix/Linux/Mac have. But there's certainly software available for Windoze, some of it free. A web search would quickly find numerous references. A spreadsheet is definitely the wrong tool for high precision calculations. Just because you could perform certain calculations in a spreadsheet doesn't you should.

Proof:
  1. Assume that √2 is a rational number, meaning that there exists an integer C and an integer D such that C/D = √2.
  2. If C and D share any common factors, cancel them so that we have A/B = √2 where A and B share no common factor.
  3. It follows that A²/B² = 2, so A² = 2B2².
  4. Therefore A² is even because it is equal to twice the integer B².
  5. It follows that A must be even (as squares of odd integers are never even).
  6. Because A is even, there exists an integer K that fulfills: A = 2K.
  7. Substituting 2K from step 6 for A in the second equation of step 3: (2K)² = 2B², so 4K² = 2B² and 2K² = B².
  8. Therefore B² is even because it is equal to twice the integer K², and so B must also be even.
  9. By steps 5 and 8, A and B are both even, which contradicts that they share no common factor.
  10. Our assumption that we have integers with C/D = √2 must be incorrect, hence √2 is irrational.
The discovery that √2 was an irrational number was a mathematical secret 2500 years ago. The punishment for divulging the secret was death.
Attachments
201303051751.ods
Calculations of 12^18 and 12^38 in a spreadsheet
(20.05 KiB) Downloaded 223 times
Last edited by MrProgrammer on Thu Mar 07, 2013 5:27 pm, edited 1 time in total.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Jan_J
Posts: 167
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Inaccuracy for big power function

Post by Jan_J »

Great number of popular software deal only with floating point arithmetcs. So is in the case of Calc. The double precision data type has only 15 to 16 decimal numbers; all the rest is scaling, being a kind of “digital zooming”, informally speaking. But this is enough for many practical purposes.
In the Open Source world there is still a number of software that can keep any precision of integer arithmetics. E.g. Python has this feature, as long as you deal with integers. It has also a link to gmp -- the Gnu Arbitrary Precision library that allows you to declare your own number of digits in floating point calculations -- despite the fact that Python's builtin float data type is double precision type.
Maxima is Open Source symbolic calculus toolbox. It generally handles rational arithmetics that can be converted to any precision decimals.
JJ ∙ https://forum.openoffice.org/pl/
LO (7.6) ∙ Python (3.11|3.10) ∙ Unicode 15 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
Post Reply