Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22031

Why is the math in my code different when compiling with MSVC versus GCC? [closed]

$
0
0

Update

I haven't figured out why this issue is happening, but I managed to eliminate it by building the executable as RelWithDebInfo. That is, a release version with debug information. This post gave me the idea.

Original Post

I have a program that does a bunch of floating-point math with doubles. There's nothing too crazy, but there is some trig (sin, cos, tan), some division, and some multiplication. I've made sure that everything is either explicitly declared as double or, if it's a constant like 1.f, that it's explicitly cast as a double.

Anyways, I have some unit tests that check that some computed values are within 1.0% of the expected value. When I build and run my program on Linux using GCC, my unit tests pass. When I switch to MSVC on the same machine, however, about half of my tests fail.

I understand that C++ does not enforce rules for how numeric values should be stored. That is, any given C++ implementation doesn't need to conform to IEEE754 floating point rules. Given that information, is it possible to force MSVC to 'act like' GCC, at least in regards to math?

I've spent a lot of time looking at the MSVC compiler options, and none of them really seemed relevant. I'm not even really sure how to investigate the issue.

I've tried the following options for MSVC:

/arch:AVX2

/fp:precise

/fp:strict

But neither of them produced results.

Additional Information
Language Information
C++17

Build System
CMake Version 3.20.3
MSVC Version 19.32.31329.0GNU Version 9.4.0

Platforms
Ubuntu Focal 20.04.4 LTS
Windows 10 Pro, Version 21H2

Hardware
Intel i7-10510U, x64-based processor

Desired Behavior
My desired behavior is to have my GCC compiler and my MSVC compiler produce results that are nearly the same. Specifically, I want both to produce results that are within 1% of the expected results for certain mathematical operations.

Minimal Reproducible Example

// coming soon

Links
MSVC Compiler Options
GCC Compiler Options


Viewing all articles
Browse latest Browse all 22031

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>