I'm on MacOS, and am following the guide Using the Intel® Math Library to implement the first example:
// real_math.c
#include <stdio.h>
#include <mathimf.h>
int main() {
float fp32bits;
double fp64bits;
long double fp80bits;
long double pi_by_four = 3.141592653589793238/4.0;
// pi/4 radians is about 45 degrees
fp32bits = (float) pi_by_four; // float approximation to pi/4
fp64bits = (double) pi_by_four; // double approximation to pi/4
fp80bits = pi_by_four; // long double (extended) approximation to pi/4
// The sin(pi/4) is known to be 1/sqrt(2) or approximately .7071067
printf("When x = %8.8f, sinf(x) = %8.8f \n", fp32bits, sinf(fp32bits));
printf("When x = %16.16f, sin(x) = %16.16f \n", fp64bits, sin(fp64bits));
printf("When x = %20.20Lf, sinl(x) = %20.20Lf \n", fp80bits, sinl(fp80bits));
return 0;
}
As directed in the guide, I compile with:
icc real_math.c
When I execute, I get:
When x = 0.78539819, sinf(x) = 0.70710677
When x = 0.7853981633974483, sin(x) = 0.7071067811865475
When x = 0.78539816339744830952, sinl(x) = 0.00000000000000000000
I've searched wide and far, and all examples seem to show that this should be trivial. What am I missing?
I tried to pass -long_double
to icc
, but nothing changes.
Ok, I figured it out. This problem was caused by an error on my part due to the omission of 1 character:
L
. Specifically, somehow in my own code on my computer I had this:and the correct code is this (note
%20.20f
vs%20.20Lf
):I'm not even sure how this could have happened, but this was the root cause.
Thank you everyone for your quick answers, and thanks @PeterCordes for linking me to godbolt.