-1

Situation: I'm running identical calculations in two separate languages (C++ & Scilab). The results are varying slightly when using transcendental function like sin() and exp(). I'm assuming this small difference is due to the following:

The IEEE standard does not require transcendental functions to be exactly rounded because of the table maker's dilemma.

Source: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Question: Is there a way for me to determine which (if either) of these languages is more accurate on my specific calculations, without manually performing the calculation myself?

I was thinking of simply putting the calculations into Wolfram Alpha and seeing which of the two holds accurately longer. Unfortunately, I'm waiting on this question (about the accuracy of Wolfram Alpha) to be answered.

Note: After posting this question I'm still going to attempt the Wolfram way mentioned above, but is there some easier, more efficient way to determine which of the two languages is outputting more accurate results?

Community
  • 1
  • 1
Paul Warnick
  • 903
  • 2
  • 13
  • 26
  • Do note that in C++ is not required to use IEEE 754 so if Scilab does then you may not be comparing apples to apples. – NathanOliver Jun 17 '16 at 19:31
  • What standard do you have to compare them to? A known function or closed form physics solution? IEEE standard won't be more accurate than 17 digits for double precision. – duffymo Jun 17 '16 at 19:31
  • @NathanOliver I'm talking strictly doubles and transcendental functions in C++, does IEEE 754 at least apply to those? – Paul Warnick Jun 17 '16 at 19:33
  • @PaulWarnick `double` does not have to be a IEEE 754 floating point number. – NathanOliver Jun 17 '16 at 19:34
  • 1
    Could you compare them with the same calculations using an arbitrary precision arithmetic library like https://gmplib.org/ ? – Galik Jun 17 '16 at 19:36
  • @NathanOliver Do you mean by programmer choice? Meaning you can choose whether your doubles follow IEEE. Or do you mean by default doubles in C++ do not comply with IEEE? – Paul Warnick Jun 17 '16 at 19:36
  • @Galik Yes! That's actually a really good idea. But is there an easier way? For example, is Wolfram more accurate than IEEE standards? (If it is, it would save me the trouble of setting up the precision library). – Paul Warnick Jun 17 '16 at 19:39
  • @PaulWarnick It is up to the implementation. The compiler you use will decide which floating point representation is used. – NathanOliver Jun 17 '16 at 19:41
  • @duffymo I don't exactly follow your comment, would you be able to clarify? A while ago I read a stack overflow post mentioning that certain transcendental functions in C++ can have better accuracy than Matlab ones. I'm trying to determine if the same applies for Scilab vs C++. (I'm trying to locate the post now) – Paul Warnick Jun 17 '16 at 19:42
  • @NathanOliver I'm using gcc, are you aware if it follows IEEE? – Paul Warnick Jun 17 '16 at 19:43
  • I'm assuming that you're using transcendental functions to calculate something else (e.g. trig functions for FFT, etc.) I'm asking if you have something that will allow you to decide if C++ or Matlab is doing a better job of representing that larger problem. – duffymo Jun 17 '16 at 19:44
  • @duffymo Unfortunately, I do not. I'm attempting to compare the overall accuracy of just one use of a function. For example sin() in C++ vs sin() in Scilab. – Paul Warnick Jun 17 '16 at 19:46
  • 1
    @PaulWarnick See: https://gcc.gnu.org/wiki/FloatingPointMath – NathanOliver Jun 17 '16 at 19:48
  • I'll ask again: What will your standard for accuracy be? Yet another implementation from a computer? A table? A graph? How can you look at either one and conclude "This one is better than the other"? – duffymo Jun 17 '16 at 19:58
  • @duffymo Essentially, I'm looking for a way to find the standard for accuracy without manually calculating it myself (i.e. on a piece of paper). For example, if C++ calculates a number to be: 0.1234 and Scilab (using the same equations) calculates the number to be 0.1236. I'm trying to find the easiest way to determine which of these two is more accurate. For example, if Wolfram / GMP calculates the number to be 0.1234 (assuming Wolfram / GMP produces the correct result) then I can say that C++ has produced the more accurate result. – Paul Warnick Jun 17 '16 at 20:04
  • Can't be done. "Assuming" - what do you think Wolfram is using? IEEE standard floating point numbers is my guess. Even if you could calculate it on a sheet of paper, what would you use to do that calculation? A calculator using IEEE standard floating point numbers? You see my point. I'm guessing that both of your candidates are doing better than three digits of accuracy. – duffymo Jun 17 '16 at 20:05
  • @duffymo I understand the issue with Wolfram, thus my reason for mentioning: http://webapps.stackexchange.com/questions/94892/how-accurate-is-wolfram-alpha in the question. It was just an example saying IF Wolfram produces an accurate result. Why would the use of an arbitrary precision library like GMP not work? – Paul Warnick Jun 17 '16 at 20:33
  • I'm concerned with the premise of this question, on a number of grounds. The terms "accuracy" and "precision" are being used interchangeably, and they are different things. A language, in itself, does not affect accuracy or precision - they result from combination of things like floating point representation and algorithms used to do the calculations (which may or may not be constrained by choice of language). More importantly, they depend on the problem being solved - you may find that one algorithm gives greater accuracy than another with some set of inputs, but not with another set. – Peter Jun 17 '16 at 22:35

2 Answers2

3

Technically speaking, there is a preliminary discussion on this: precision is different than accuracy. Accuracy is a measurement of how close your calculation is to the true value, whatever it is; precision is a measurement of how close are many calculations to each other. You understand better if you think in terms of probability distribution of a set of calculation results. Therefore, you could have poor accuracy with high precision, for example. The title of your post is about accuracy, but in the body you mention precision. I guess you meant accuracy.

That said, there is no general answer. There are many methods you could use, actually infinite algorithms could fit your needs. You need:

  • a library like GMP, as already suggested
  • paper and pencil to evaluate the accuracy of your algorithm

Why this? You need to evaluate your sin(x) with higher accuracy than the two results you want to compare. And algorithms for transcendental functions are series. So you have to estimate how the algorithm error - and in some cases, still the representation errors - influence you calculation. At that point, implement the method of your choice, and evaluate the distance between your result and the other two, being sure that the total error affecting your result is however smaller than some controllable constant.

As an example, evaluating exp(10) can be done using the usual exponential Taylor expansion around zero, and using many terms; but if you use the same method for exp(-10), you may fail, as its expansion has alternating sign terms. This means that you may have cancellation effects that could accumulate. You do not want this, otherwise you may be precise, but biased, inaccurate in your calculation. Usually GMP is used because it is designed for this.

So, for example, you may discover that Scilab is best in calculating exp(-10), and C++ in exp(10), just because they could use different implementations.

In summary: use GMP, and look for an accurate algorithm. This may work, or be the only solution, if you need an accurate result of some particular Fourier summation, or a series in which other transcendental terms have to be evaluated, for example. But if you just need this answer for simple sin and exp, you should look for tables, and that's it.

fedino
  • 913
  • 1
  • 9
  • 15
1

If you want consistent accuracy of transcendental math functions I suggest you write your own.

There are various methods for implementing functions such as sin. Some use a table of values (e.g. small embedded systems platforms) to hardware calculations.

The implementation of the transcendental math functions is usually dependent on the language implementor.

BTW, the general implementation is a middle ground between space, speed and accuracy. A highly accurate version may require a lot of space or a lot of time.

At least if you have you own implementation, you can control the accuracy rather than guessing at the accuracy of a language's implementation.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • You can use whatever floating point number format you want. The point is the implementation of the algorithm. You could have a floating point format with 1024 bits of mantissa, but if the algorithm is only good to 16 bits of mantissa, you'll need a better algorithm. – Thomas Matthews Jun 17 '16 at 21:29