Created
February 24, 2012 19:15
-
-
Save michael-galpin/1903068 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <float.h> | |
#define INF 1.0/0.0 | |
float calcMinFloat(); | |
float calcFloatErr(); | |
float calcMaxFloat(); | |
int main() | |
{ | |
float f; | |
f = FLT_MIN; | |
printf("Min float %e\n", f); | |
f = calcMinFloat(); | |
printf("Min float calculated %e\n", f); | |
f = calcFloatErr(); | |
printf("Float epsilon = %e\n",f); | |
f = FLT_MAX; | |
printf("Max float %e\n", f); | |
f = calcMaxFloat(); | |
printf("Max float calculated %e\n", f); | |
return 0; | |
} | |
float calcMinFloat() | |
{ | |
float a,b,c; | |
a = 1.0; | |
b = 0.0; | |
while (a - b != 0.0){ | |
c = 0.5*(a - b); | |
if (c < 0.0){ | |
b = b - c; | |
} else if (c == 0.0) { | |
b = a; | |
} else { | |
a = a -c; | |
} | |
} | |
b = calcFloatErr(); | |
return a/b; | |
} | |
float calcFloatErr() | |
{ | |
float err; | |
err = 1.0/2.0; | |
while ((float) (1.0 + err/2.0) != 1.0){ | |
err = err / 2.0; | |
} | |
return err; | |
} | |
float calcMaxFloat() | |
{ | |
float a, b, err; | |
a = b = err = 1.0 + calcFloatErr(); | |
b = a * err; | |
while (b != a && b != INF){ | |
a = a * err; | |
b = a * err; | |
} | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment