0

I have the following piece of c++ code:

void update(const int step, const int total) const
{
   double s = static_cast<double>(step);

   double t = static_cast<double>(total);

   std::cout << s/t <<"------\n";

   // etc...
}

I am using the intel c++ compiler with the -fp-trap=all flag activated. When running the code through gdb I get the following error:

Program received signal SIGFPE, Arithmetic exception.
0x000000000040ee07 in NilDa::progressBar::update (this=0x7fffffffbc9c, step=1, total=60000) at /home/d2d/dev/NilDa/sources/utils/progressBar.h:69
69  std::cout << s/t <<"------\n";

I don't really understand what is going on. The division seems to be well defined.

Dante
  • 11
  • 2
  • the inputs are step=1, total=60000 (as reported by gdb) therefore s=1.0, t=60000.0 – Dante Sep 17 '20 at 22:22
  • 2
    Also, we have no idea, when or where this code is called. How about a simple `main` program that simply divides 1.0 by 60000.0, to see if there is anything wrong with that simple case. – PaulMcKenzie Sep 17 '20 at 22:23
  • @cigien The values are visible within the gdb output ;) Dante: Which compiler do you use? – WolverinDEV Sep 17 '20 at 22:23
  • PS If you're using the intel C++ compiler a quick lookup here will solve your question (`Enables or disables the IEEE trap for invalid operation.`): https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/floating-point-options/fp-trap-all-qfp-trap-all.html – WolverinDEV Sep 17 '20 at 22:24

1 Answers1

2

I'm assuming you're using the Intel C++ compiler (I'm not aware of any other compiler having such a flag). If so you can take a look here: https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/floating-point-options/fp-trap-qfp-trap.html#

As stated within that documentation the argument all inherits the trap for inexact results ([no]inexact)

Enables or disables the IEEE trap for inexact result.

Since 1/60000 can not be represented by an floating point number the result is inexact (1.66667e-05).

WolverinDEV
  • 1,494
  • 9
  • 19
  • @WolvernDEV As reported in the question I am using the Intel c++ compiler. I get the point about the approximation of 1/6000, but is that true also for a double? I am not using a float, but a double. – Dante Sep 17 '20 at 22:43
  • Well regardless of a double float or whatever, 1/60000 is a periodic result (1/6 * 1e-5). 1/6 can not be represented as any floating point or fixed point number ;) – WolverinDEV Sep 17 '20 at 23:07