Strange! linux & windows vsprintf float num to file - c++

use vsprintf to write the content to file.
output format is:
"tt2:%f, tt2:%x", tt2, *((int *)&tt2)
linux:
gcc 4.4.5: -O2 -ffloat-store
In linux.in file is like this:
tt2:30759.257812, tt2:46f04e84
windows:
vs2005 sp1: /O2 Precise (/fp:precise)
In windows. in file is like this:
tt2:30759.257813, tt2:46f04e84
Why that is different?
==================================
I have find the reason for my case.
In windows, I use the ofstream to output to file. It'c c++ lib.
In linux, I just use write to output to file. It's c lib.
When I use ofstream in linux, the output is the same.
After all, thanks for everyone~

Floating-point numbers are stored in the computer in binary. When printing them into decimal floating-point, there are multiple correct representations for them. In your case, both of them are correct, as both of them convert back to the original binary floating-point value. Look at the output of this file, which I compiled using GCC:
#include <stdint.h>
#include <stdio.h>
int main()
{
float a = 30759.257812f;
float b = 30759.257813f;
printf("%x\n%x\n", *(uint32_t *)&a, *(uint32_t *)&b);
}
Output:
46f04e84
46f04e84
Therefore, an implementation of printf and friends may choose to display any of the two decimal floating-point numbers.

Related

How Does Compiler know addition , subtraction operators while compiling the code?

I have made the simple program in cplus like this -
main()
{
int a=5;
int b=8;
int c;
c= a+b;
cout<<c<<endl;
return 0;
}
I get the output as 13 which is correct.
I want to know How g++ compiler knows about "+" operator ?
Is there any method defined in header file for this operator , like the other keywords are defined in standard libraries.
There is no method for basic types such as int short char double etc. Basic operation for these types is built into compiler. So compiler when it sees these operations on basic types converts it directly to assembly. So here is an example:
If you want to see assembly output do following gcc -S myfile.c or for c++ g++ -S myfile.cpp

Different results for pow on x86 and x64

I am wondering why I am observering different results when using pow on x86 and x64 respectively. In our application we control the floating point rounding mode, which has worked fine on both Windows and Linux 32 bit.
#include <cmath>
#include <cstdio>
#include <cfloat>
void checkEqual(double expected, double observed) {
if (expected == observed) {
printf("Expected %.23f, got %.23f\n", expected, observed);
}
else {
printf("ERROR: Expected %.23f, got %.23f\n", expected, observed);
}
}
int main(int argc, char **argv) {
unsigned ret, tmp;
_controlfp_s(&ret, 0, 0);
_controlfp_s(&tmp, RC_DOWN, MCW_RC);
checkEqual(2048.0, pow(2.0, 11.0));
checkEqual(0.125, pow(2.0, -3.0));
return 0;
}
Compiling and running with Visual Studio 2015 (2012 gives the same result) gives me the following output
x86:
Expected 2048.00000000000000000000000, got 2048.00000000000000000000000
Expected 0.12500000000000000000000, got 0.12500000000000000000000
x64:
ERROR: Expected 2048.00000000000000000000000, got 2047.99999999999977262632456
ERROR: Expected 0.12500000000000000000000, got 0.12499999999999998612221
Can anyone explain the differences? I know that inherently, floating point calculations are not exact, but for these specific values, I would expect the function to produce the same result regardless of rounding mode.
I investigated this some more and found that the real issue is not how pow is implemented but rather that x86 and x64 hardware are different as Hans Passant suggested.
There are many different implementations of pow possible. For instance, pow(x,y) is exp(y*ln(x)). As you can imagine, exp(11.0*ln(2.0)) can suffer from internal rounding errors even though x and y are exactly representable.
However, it's quite common to call pow with integer arguments, and therefore some libraries have optimized paths for those cases. E.g., pow(x,2*n) is pow(x,n) squared, and pow(x, 2n+1) is x*pow(x, 2n).
It seems your x86 and x64 implementations differ in this respect. That can happen. IEEE only promises precise results for +-*/ and sqrt.

Using long doubles on windows

I have an application where I absolutely must use long double data type due to catastrophic truncation error when doing math with double precision. My testing procedures are going crazy because on windows long double with Visual Studio is just an alias to double, while on linux and OSX, long double is a real long double with nominally precision of 1e-19.
On mingw (windows port of GCC) is where I am confused. Mingw claims that LDBL_EPSILON has a precision of 1e-19, but googleing suggests that the mingw uses the c runtime that is actually just the microsoft c runtime which doesn't support real long doubles. Can anyone shed any light here?
EDIT: The crux of the problem is this: on mingw, if I call the math function log(long double x), is this just an alias to log(double x)? In either case, how could I write my own script to test this behavior and/or test for it?
Following code
#include <iostream>
#include <cmath>
int main(void)
{
long double y = 2.0L;
std::cout << sizeof(y) << std::endl;
long double q = sqrt(y);
std::cout << q << std::endl;
return 0;
}
produced output 16 1.41421, so far so good
Ran it throw preprocessor (-E option) and found out that internal, but different from double sqrt() function were called
using ::sqrt;
inline constexpr float sqrt(float __x)
{ return __builtin_sqrtf(__x); }
inline constexpr long double sqrt(long double __x)
{ return __builtin_sqrtl(__x); }
Same for log(), sin(), you name it
Thus, I believe MinGW support long double format in arithmetics as well as in math.functions, and this support is built-in, not libquadmath based
Just tried with MinGW (MinGW distro from nuwen, gcc/g++ 4.9.1, 64bit)
Following program
#include <iostream>
int main(void)
{
double x = 1.0;
long double y = 2.0L;
std::cout << sizeof(x) << std::endl;
std::cout << sizeof(y) << std::endl;
}
produced
8
16
I would guess, long double is supported and is different from standard double, thus
your computations might produce desired result
I've heard there are problems with printing long doubles on Windows due to using MS old runtime.
You might have to use casts or roll your own output routines

c++ - Defining const variable not working across header file

I am currently working on a little program for the Raspberry Pi. It involves some 7 segment displays. To be able to write more programs for this display I decided to extract the code that directly communicates with the GPIO's to a seperate .cpp and .h file.
Since the number of digits is variable I used a variable for that. The digits themselves are stored in a array consiting out of 8 bit integers.
This is what my setup looks like:
7_segment.h:
extern const uint8_t numDigits;
extern uint8_t digits[];
7_segment.cpp:
#include "7_Segment.h"
uint8_t digits[numDigits] = {0x00}; // Line 7
And the file with the "actual" program:
clock.cpp:
#include "7_Segment.h"
const uint8_t numDigits = 4;
When I execute
g++ clock.cpp 7_segment.cpp -o clock -std=c++0x -lwiringPi
I get this output:
7_segment.cpp:7:27: error: array bound is not an integer constant before ‘]’ token
What can I do to resolve this?
The numDigits=4 is defined in the clock.cpp. The 7_segment.cpp has no idea about the size of array digits[]. Array sizes need to be compile-time constants, so you'll need to put the actual number in 7_segment.cpp or as a compile-time constant in 7_segment.h instead.

Why round() is known by mingw but not by visual studio compiler

Sample code which is valid and gets compiled by gcc but not by VS compiler:
#include <cmath>
int main()
{
float x = 1233.23;
x = round (x * 10) / 10;
return 0;
}
but for some reason, when I am compiling this in Visual Studio I get an error:
C3861: 'round': identifier not found
I do include even cmath as someone suggested here: http://www.daniweb.com/software-development/cpp/threads/270269/boss_loken.cpp147-error-c3861-round-identifier-not-found
Is this function in gcc only?
First of all, cmath is not guaranteed to bring round into the global namespace, so your code could fail, even with an up-to-date, standards compliant C or C++ implementation. To be sure, use std::round (or #include <math.h>.)
Note that your C++ compiler must support C++11 for std::round (<cmath>). A C compiler should support C99 for round (from <math.h>.) If your version of MSVC doesn't work after the fix I suggested, it could simply be that that particular version is pre-C++11, or is simply not standards compliant.
You also can use a boost library:
#include <boost/math/special_functions/round.hpp>
const double a = boost::math::round(3.45); // = 3.0
const int b = boost::math::iround(3.45); // = 3
Visual Studio 2010 (C99) doesn't support round but ceil and floor functions, so you may want to add your own function like;
long long int round(float a)
{
long long int result;
if (a-floor(a)>=0.5)
result = floor(a)+1;
else
result = floor(a);
return result;
};
The std::round nor #include <math.h> supported at the VS 2010 according to my experience.
I would use floor twice to get the correct rounded value,
long long int round(float a)
{
long long int result = (long long int)floor(a);
return result + (long long int)floor((a-result)*2)
};