-2

When I'm compiling the following piece of C++ code using Clang++ and G++

void ScaleFactor(float32_t scale, int32_t &factor) {
    factor = floor(log2(abs(scale))+1)
    cout << factor << endl;
}

where the value of scale equals 0.234375 and factor is passed by reference to an integer variable in another function, I'm getting the following different outputs for factor while debugging using GDB

(int32_t &) @0x7fffffffdaa0: -2147483648 (Clang++)
(int32_t &) @0x7fffffffda9c: -2 (G++)

But, on replacing abs() with fabs() each output matches with the desired output

(int32_t &) @0x7fffffffdb90: -2 (Clang++)
(int32_t &) @0x7fffffffdddc: -2 (G++)

What might be the reason for this discrepancy?

therealanshuman
  • 150
  • 1
  • 1
  • 12
  • 4
    What values are you calling the function with? – DeducibleSteak Feb 18 '20 at 20:40
  • Sorry, forgot to mention that! Value scale is 0.234375 and factor is just a reference to an integer variable. – therealanshuman Feb 18 '20 at 20:46
  • Recommendation: Cut the program down to `ScaleFactor` and a simple `main` that only exists to call `ScaleFactor` with your fixed inputs. If you get the same results, post this little program so we can all run it and see. If the problem goes away, yousa got a bug, but it's not where yousa thinks it is. – user4581301 Feb 18 '20 at 20:54
  • Where does `float32_t` come from? – user4581301 Feb 18 '20 at 21:01
  • 1
    Where is the [mcve], command line, warnings, etc? This is relevant, the answer may depend on your exact list of #include. – Marc Glisse Feb 18 '20 at 21:01
  • Try and add this: `cout << abs(0.5f) << endl;` right after your output. If you get 0, you're surely including the wrong math functions. – DeducibleSteak Feb 18 '20 at 21:18

2 Answers2

1

See the answer to this post. abs is generally for integers and fabs is for floating point types. However, it looks like G++ implementation of abs will handle floats, too.

fstop_22
  • 971
  • 5
  • 9
1

Does this code produce the same results? It does not seem to, for me: https://godbolt.org/z/Wj9v6j

#include <iostream>
#include <cmath>

using namespace std;

using float32_t = float;

void ScaleFactor(float32_t scale, int32_t &factor) {
    factor = floor(log2(abs(/*(int)*/scale))+1);
    cout << factor << endl;
}


int main() {
    int32_t result;
    ScaleFactor(0.234375f, result);
    return 0;
}

If it does not, are you sure that something else is not going wrong here? Maybe you are including the C versions of the math functions, where there is a difference between abs and fabs, when building with clang?

The weird result you get is exactly what I get if I cast the float to int when passing it to abs, exactly what you would expect if you were using the C version of abs.

DeducibleSteak
  • 1,398
  • 11
  • 23
  • And I'll bet you $42 more that, when it does, what's left in its place will have the same exact value as it would have returned. – DeducibleSteak Feb 18 '20 at 21:09
  • I just took your godbolt link, replaced cmath with math.h, removed all the iostream stuff, removed the `using namespace std`, and `std::abs` [still got called](https://godbolt.org/z/C2rkuX). Wasn't expecting that. Was expecting a failure to compile (no `int32_t`). Wonder what's up. – user4581301 Feb 18 '20 at 21:19
  • Well. That was easy. Inside math.h is a `# include `. Probably a cstdint include in there somewhere. – user4581301 Feb 18 '20 at 21:22