Created
July 20, 2025 18:14
-
-
Save nickodell/8838119e3a7579551977116b232a0252 to your computer and use it in GitHub Desktop.
Comparison of Fortran and C for division by 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// command gcc -Wall -Wextra -pedantic test_sgnd.c -lm -o test_sgnd_c && ./test_sgnd_c | |
#include <stdio.h> | |
#include <math.h> | |
// Test sgnd = dp*(dx/ABS(dx)) for all combinations of [1, 0, -0, 1] | |
int main(void) | |
{ | |
const double dp[4] = { 1.0, 0.0, -0.0, -1.0 }; | |
const double dx[4] = { 1.0, 0.0, -0.0, -1.0 }; | |
puts(" dp dx sgnd"); | |
for (int i = 0; i < 4; ++i) { | |
for (int j = 0; j < 4; ++j) { | |
double sgnd = dp[i] * (dx[j] / fabs(dx[j])); | |
//double sgnd = copysign(dp[i], dx[j]); | |
printf("%8.2f %8.2f %8.2f\n", dp[i], dx[j], sgnd); | |
} | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C Command gfortran -std=legacy -Wall -Wsurprising -Waliasing -Wconversion -Wimplicit-interface -pedantic -fcheck=all -g -o test_sgnd test_sgnd.f && ./test_sgnd | |
PROGRAM TEST_SGND | |
C Test sgnd = dp*(dx/ABS(dx)) for all combinations of [1, 0, -0, 1] | |
IMPLICIT NONE | |
DOUBLE PRECISION dp(4), dx(4), sgnd | |
INTEGER i, j | |
C Values (note −0.0 to exercise negative-zero on IEEE targets) | |
DATA dp / 1.0D0, 0.0D0, -0.0D0, -1.0D0 / | |
DATA dx / 1.0D0, 0.0D0, -0.0D0, -1.0D0 / | |
PRINT *, ' dp dx sgnd' | |
DO 20 i = 1, 4 | |
DO 10 j = 1, 4 | |
sgnd = dp(i) * (dx(j) / ABS(dx(j))) | |
PRINT '(F8.2,1X,F8.2,1X,F8.2)', dp(i), dx(j), sgnd | |
10 CONTINUE | |
20 CONTINUE | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment