In: Computer Science
Where is the infinite loop in the code?
Input for the code 1 2.5 2 40200000 1 0 2 80400000 2 ffffffff 3
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int count = 31;
void toBinary(int num, int n){
for(int i = 1; i < n; i++){
if((int)(num/pow(2,(n-i))) > 0){
num = num - pow(2,(n-i));
printf("1");
count--;
}else{
printf("0");
count--;
}
}
}
char checkSign (int sign)
{
char new_sign;
if (sign == 0)
{
new_sign = '+';
return new_sign;
}
else
{
new_sign = '-';
return new_sign;
}
}
void oddCheck(int num){
if(num%2 == 0){
printf("0\n");
count--;
}else{
printf("1\n");
count--;
}
}
void dec2IEEE() {
float dec_num;
short sign;
short dec_exp;
printf("\nPlease enter a decimal number: ");
scanf("%f", &dec_num);
/*if dec_num is 0 print out 32-bit IEEE of 0 (32 zeroes)*/
if (dec_num == 0.0f) {
for (int i = 0; i < 31; ++i) {
printf("0");
}
printf("\n");
}
/*check dec_num sign*/
if (dec_num > 0) {
sign = 0;
} else {
sign = 1;
dec_num = abs(dec_num);
}
printf("\nSign : %d\n", sign);
/*find dec_expo*/
dec_exp = floor(log(dec_num) / log(2));
dec_num = dec_num / pow(2, dec_exp);
/*ensure that there is a 1 in front of the decimal*/
while(dec_num < 1.0){
dec_num = dec_num * 2;
dec_exp--;
}
/*create binary exponent first line creates copy of the value of
dec_exp + 127*/
printf("%d\n", dec_exp);
printf("Exponent: ");
int exp_copy = dec_exp += 127;
toBinary(dec_exp, 8);
oddCheck(exp_copy);
/*create significand in binary*/
dec_num -= 1;
int power = 0;
while(dec_num != (float)((int)(dec_num))){
dec_num *= 2;
power++;
}
int dec_num_copy = dec_num;
printf("Significand: ");
toBinary(dec_num, power);
oddCheck(dec_num_copy);
while(count > 0){
printf("0");
count--;
}
}
void IEEE2Dec(){
int ieee_num;
int ieee_sign;
int ieee_exp;
float ieee_sig;
float ieee_dec;
printf("\nPlease Enter IEEE number: ");
scanf("%x", &ieee_num);
if(abs(ieee_num) == 0x00000000){
printf("You entered 0\n");
return;
}
if(ieee_num == 0x7f800000){
printf("You entered infinity\n");
return;
}
if(ieee_num == 0xff800000){
printf("You entered negative infinity\n");
return;
}
if((ieee_num & 0x7fffffff) > 0x7f800000){
printf("You entered NAN\n");
return;
}
ieee_sign = floor (ieee_num / pow (2, 31));
printf ("Sign: %c", checkSign (ieee_sign));
ieee_exp = floor ((ieee_num & 0x7F800000) / pow (2,
23));
ieee_exp -= 127;
printf ("\nExponent: %d", ieee_exp);
ieee_sig = (ieee_num & 0x007fffff) / pow (2, 23);
if (ieee_exp < 0)
{
ieee_dec = ieee_sig;
printf ("\nDenormalized Decimal: %c%f*2^(%d)", checkSign
(ieee_sign),
ieee_dec, ieee_exp);
return;
}
ieee_sig += 1;
printf ("\nNormalized Decimal: %f", ieee_sig);
ieee_dec = ieee_sig * pow (2, ieee_exp);
printf ("\nDecimal Format: %c%f", checkSign (ieee_sign),
ieee_dec);
}
int main() {
int true = 1;
while(true){
printf("Please Enter an Option \n 1. Decimal to IEEE \n 2. IEEE to
Decimal \n 3. Exit\n");
int option;
scanf("%d", &option);
switch(option){
case 1:
dec2IEEE();
break;
case 2:
IEEE2Dec();
break;
case 3:
true = 0;
break;
}
}
return 0;
}
// an infinte loop is the one which never ends
// in this code while loop in the main function is the inifinite loop makrked in bold
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int count = 31;
void toBinary(int num, int n){
for(int i = 1; i < n; i++){
if((int)(num/pow(2,(n-i))) > 0){
num = num - pow(2,(n-i));
printf("1");
count--;
}else{
printf("0");
count--;
}
}
}
char checkSign (int sign)
{
char new_sign;
if (sign == 0)
{
new_sign = '+';
return new_sign;
}
else
{
new_sign = '-';
return new_sign;
}
}
void oddCheck(int num){
if(num%2 == 0){
printf("0\n");
count--;
}else{
printf("1\n");
count--;
}
}
void dec2IEEE() {
float dec_num;
short sign;
short dec_exp;
printf("\nPlease enter a decimal number: ");
scanf("%f", &dec_num);
/*if dec_num is 0 print out 32-bit IEEE of 0 (32 zeroes)*/
if (dec_num == 0.0f) {
for (int i = 0; i < 31; ++i) {
printf("0");
}
printf("\n");
}
/*check dec_num sign*/
if (dec_num > 0) {
sign = 0;
} else {
sign = 1;
dec_num = abs(dec_num);
}
printf("\nSign : %d\n", sign);
/*find dec_expo*/
dec_exp = floor(log(dec_num) / log(2));
dec_num = dec_num / pow(2, dec_exp);
/*ensure that there is a 1 in front of the decimal*/
while(dec_num < 1.0){
dec_num = dec_num * 2;
dec_exp--;
}
/*create binary exponent first line creates copy of the value of
dec_exp + 127*/
printf("%d\n", dec_exp);
printf("Exponent: ");
int exp_copy = dec_exp += 127;
toBinary(dec_exp, 8);
oddCheck(exp_copy);
/*create significand in binary*/
dec_num -= 1;
int power = 0;
while(dec_num != (float)((int)(dec_num))){
dec_num *= 2;
power++;
}
int dec_num_copy = dec_num;
printf("Significand: ");
toBinary(dec_num, power);
oddCheck(dec_num_copy);
while(count > 0){
printf("0");
count--;
}
}
void IEEE2Dec(){
int ieee_num;
int ieee_sign;
int ieee_exp;
float ieee_sig;
float ieee_dec;
printf("\nPlease Enter IEEE number: ");
scanf("%x", &ieee_num);
if(abs(ieee_num) == 0x00000000){
printf("You entered 0\n");
return;
}
if(ieee_num == 0x7f800000){
printf("You entered infinity\n");
return;
}
if(ieee_num == 0xff800000){
printf("You entered negative infinity\n");
return;
}
if((ieee_num & 0x7fffffff) > 0x7f800000){
printf("You entered NAN\n");
return;
}
ieee_sign = floor (ieee_num / pow (2, 31));
printf ("Sign: %c", checkSign (ieee_sign));
ieee_exp = floor ((ieee_num & 0x7F800000) / pow (2,
23));
ieee_exp -= 127;
printf ("\nExponent: %d", ieee_exp);
ieee_sig = (ieee_num & 0x007fffff) / pow (2, 23);
if (ieee_exp < 0)
{
ieee_dec = ieee_sig;
printf ("\nDenormalized Decimal: %c%f*2^(%d)", checkSign
(ieee_sign),
ieee_dec, ieee_exp);
return;
}
ieee_sig += 1;
printf ("\nNormalized Decimal: %f", ieee_sig);
ieee_dec = ieee_sig * pow (2, ieee_exp);
printf ("\nDecimal Format: %c%f", checkSign (ieee_sign),
ieee_dec);
}
int main() {
int true = 1;
while(true){
printf("Please Enter an Option \n 1. Decimal to IEEE \n 2. IEEE to
Decimal \n 3. Exit\n");
int option;
scanf("%d", &option);
switch(option){
case 1:
dec2IEEE();
break;
case 2:
IEEE2Dec();
break;
case 3:
true = 0;
break;
}
}
return 0;
}