0

I typed abs(sin(pi)) into MATLAB. I expected it to give me the absolute value of pi, hoping to mitigate the natural rounding error with 'abs' (absolute value). I still get a rounding error. What should I do instead and why am I wrong?

Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
Elsavador
  • 17
  • 6
  • I don't understand the question. On my machine `sin(pi)` returns `1.2246e-16`; a reasonable outcome as it is approximately `eps(1)` from zero. `abs(sin(pi))` returns the same number as it should, since the number is already positive. Where is the problem in any of this? – Colin T Bowers Jan 20 '13 at 22:18
  • Besides, abs shouldn't cause any rounding error whatsoever, no matter the argument. – Gato Jan 20 '13 at 22:21
  • I was hoping for the result sin(pi)=0 I am confused as to why it approximates eps(1) at all By having abs() I hoped to find the absolute version of the number to which 0 was being approximated "Since the number is already positive" I have now tried sin(abs(0)) thinking that my placement of abs might fix it to no avail thank you for your help thus far, any further help greatly appreciated – Elsavador Jan 20 '13 at 22:23
  • Floating point error is a fact of life. You're certainly not going to get rid of it using the `abs` function. I answered a question on methods of mitigating floating point inaccuracy the other day actually, you can read up on it [here](http://stackoverflow.com/questions/14348722/numerical-issue-when-computing-complementary-projection/14350018#14350018). Also, when community members edit your question to improve the formatting and grammar, please don't edit it back to incorrect formatting and grammar. – Colin T Bowers Jan 20 '13 at 22:27
  • I've also now tried loge(sin(pi)) to try to to nullify the log(e), without avail – Elsavador Jan 20 '13 at 22:28
  • Thanks Colin, also, I am unaware of any editing to format my question correctly, and not sure how I unedited it, but thanks to whoever did it and sorry to whoever I changed - I've edited my question since posting to try and fix it, I didn't realise anything had been done. – Elsavador Jan 20 '13 at 22:29
  • Ps, I didn't see your answer when I posted my loge attempt – Elsavador Jan 20 '13 at 22:30

1 Answers1

1

Floating point numbers are always going to give you problems like this. That's why it's common to write:

if (x - TestValue < 0.000001)

instead of

if (x == TestValue)

I would recommend trying to round the value down a decimal place or two, using something like this:

x = floor(x * 1e15) / 1e15;

which rounds x down to the nearest 1e15. You could also use round or ceil. This article has some more information on similar strategies for rounding.

Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
  • Just out of curiosity, shouldn't it be easy for any math software that can handle algebra, to support whole number precision? – Elsavador Jan 20 '13 at 22:37
  • Great question! There's a fantastic answer [here](http://programmers.stackexchange.com/a/76884/79021). In short, it's all got to do with the fact that computers think and round numbers in base 2, whereas people think and round numbers in base 10, and there isn't a base 10 representation that perfectly fits inside a base 2 representation. See also [Floating Point on Wikipedia](http://en.wikipedia.org/wiki/Floating_point). – Fabian Tamp Jan 20 '13 at 22:57