0

In a function containing expensive operations such as floating-point division, sqrt or cbrt, are there any ways to specify the accuracy of the result in order reduce computation time? I.e. if I am happy to have a result that is within +/- 0.01 of the correct answer, or within 5%, is there a way to specify this?

user366202
  • 249
  • 2
  • 7
  • If you are using gcc you can pass -ffast-math as a compiler flag. – Mestkon May 29 '20 at 13:58
  • less precision is not necessarily faster, see eg here: https://stackoverflow.com/questions/4584637/double-or-float-which-is-faster – 463035818_is_not_an_ai May 29 '20 at 13:59
  • @Mestkon, thanks, I am already using -ffast-math, but I am willing for the error to be even higher for this specific function – user366202 May 29 '20 at 14:00
  • @idclev463035818 in this case I am asking about accuracy rather than precision. I'm not sure about division but I think sqrt/cbrt are based on iterative processes, so I hoped that it was possible to change the number of iterations etc to get a less-accurate result faster – user366202 May 29 '20 at 14:03
  • Maybe use a float instead of a double? Or implement one of the bit-twiddling square root solutions (there are ones for cube root too, I'm sure) – Mad Physicist May 29 '20 at 14:05

1 Answers1

2

There is no standard way to control accuracy in c++.

The best way might be to roll your own implementation based on the accuracy needs of your project. For example it is possible to use a look-up table instead of trigonometric functions, or other functions if the range of input is small. It is also possible to use a less accurate approximation of the desired function. A taylor series with few terms for example.

You can also vectorize your work using intrinsics or your GPU.

Mestkon
  • 3,532
  • 7
  • 18