mirror of
https://github.com/Comp211-SP24/lab-02-Rushilwiz.git
synced 2025-04-09 14:50:19 -04:00
finished f32
This commit is contained in:
parent
5602dbfb3f
commit
43b2db6997
32
f32.c
32
f32.c
|
@ -2,8 +2,36 @@
|
||||||
// I pledge the COMP 211 honor code.
|
// I pledge the COMP 211 honor code.
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char* argv[]) {
|
||||||
return EXIT_SUCCESS;
|
// convert a IEEE 754 single precision floating point number to a 32-bit
|
||||||
|
// integer
|
||||||
|
char input[33];
|
||||||
|
char exponent[9];
|
||||||
|
char mantissa[24];
|
||||||
|
|
||||||
|
strncpy(input, argv[1], 32);
|
||||||
|
input[32] = '\0';
|
||||||
|
|
||||||
|
strncpy(exponent, input + 1, 8);
|
||||||
|
strncpy(mantissa, input + 9, 23);
|
||||||
|
|
||||||
|
int sign = (input[0] == '1') ? -1 : 1;
|
||||||
|
|
||||||
|
long int exp = strtol(exponent, NULL, 2) - 127;
|
||||||
|
|
||||||
|
float frac = 1.0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 23; i++) {
|
||||||
|
if (mantissa[i] == '1') {
|
||||||
|
frac += pow(2, -i - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float result = sign * frac * pow(2, exp);
|
||||||
|
|
||||||
|
printf("%.4f\n", result);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user