I was debugging some code in C++ with a friend when I ran into something interesting.
I was set up on Netbeans using MinGW's GCC compiler version 4.6.2, and my friend was using DevC++ which uses MinGW's GCC version 3.4.2.
At the time I was using the setprecision function included in the iomanip standard library file.
setprecision allows you to set decimal precision in output operations.
It was a simple operation that had a small error, I was setting the precision to 2 on 118.125 which should have given me 118.13.
Instead my output was 118.12. I checked the same code on my friend's computer and noticed that his functioned fine, outputting 118.13.
It was only a minor error in the way set precision normally worked, but I assumed the problem was with the compiler version.
I could have it wrong, but I believe each implementation of a compiler, can carry minor differences in the way they implement the standard library. Although this isn't a huge issue, it could have led to larger issues with larger calculations, even if it's only off a decimal.
Like I said, I could be wrong about why this is happening, so if anyone else has an explanation I'd be interested in knowing about it.
Have you guys run into any weird inconsistencies among different common compilers for a language?
Results 1 to 6 of 6
- 13 Feb. 2013 08:32am #1
MinGW Standard Library Differences
I don't get tired.
- 14 Feb. 2013 03:09am #2
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 14 Feb. 2013 04:46am #3
Nope, setprecision only takes one parameter.
Interesting update on it though. According to a few people who have run into the same problem, the issue is that computer's can't represent floating points perfectly.
Apparently if you have a number like 5.555 the computer actually stores it as something like 5.55499999999876, which rounds down to 5.55. So adding 0.000000001 to the decimal value gives me the number I'm looking for and seems to fix the rounding problems.I don't get tired.
- 14 Feb. 2013 06:14pm #4
>I was set up on Netbeans using MinGW's GCC compiler version 4.6.2, and my friend was using DevC++ which uses MinGW's GCC version 3.4.2.
This appears to be your problem. Look at the disparity there - you're using a 4.6.x build of the recent GCC compiler w/ NetBeans (another fairly recent IDE) while your "friend" or whatever is using the heavily deprecated DevC++ IDE and an outdated GCC 3.4.x.
Just get on the same page. DevC++ sucks. He/she should be using the same or another modern C++ IDE for debugging code. Use the same compiler version because you always want to do that anyway when collaborating with others unless there's some ulterior reason for doing so otherwise.
- 14 Feb. 2013 06:37pm #5I don't get tired.
- 14 Feb. 2013 07:27pm #6