Thanks sooooooooo much for the help. It worked I know where to come when I need help. Thanks again to all of you. Is it possible that this can be broken into a function.
Thanks sooooooooo much for the help. It worked I know where to come when I need help. Thanks again to all of you. Is it possible that this can be broken into a function.
Last edited by sara.stanley; 02-07-2006 at 12:00 PM.
I got it to work as a function thanks.
B = 2^PREC * N by jumpjack
this formula can be used to print floating numbers in binary code to PREC bits
in this post i will explain how it works
similar to the bitwise operator << which shifts bits to the left. when you multiply a number by 2^PREC what happens is each bit shifts towards the left by PREC positions. below is a few examples.
the binary code for 0.75 is 0000.1100 if multiplied by say 2^1 the answer will be 1.5. 1.5 has a binary code of 0001.1000. there you can see that all the bits have been shifted by 1 position to the left.
0.75 multiplied by 2^2 is 3, which has the binary code 0011.0000. you can see the bits have been moved by 2 positions to the left.
0.75 * 2^3 would shift bits 3 positions to the left, *2^4 it will shift bits by 4 positions to the left and so on.
You can also work out what PREC should be used, in the example above it was 2. basically 2^PREC * N should give a whole number. that whole number should be as small as possible.
here is how to get PREC for 0.875
2^1*0.875 = 1.75
2^2*0.875 = 3.5
2^3*0.875 = 7
2^4*0.875 = 14
the smallest whole number we get is 7, the PREC we should use is 3 (in your program you could use a loop to find PREC)
to find the equivalent binary code of 0.875 you would find the binary code for 7.
hope this has been useful
I am trying to make this code work for hexadecimal also but I keep getting Segmentation Errors. I am adding the following conditions.
Code:int c,i; printf("Its hexadecimal equivalent is "); for (i=c;i>=0;--i) { if (num==10) printf("A"); else if (num==11) printf("B"); else if (num==12) printf("C"); else if (num==13) printf("D"); else if (num==14) printf("E"); else if (num==15) printf("F"); else printf("%f",num); } return; }
Maybe
EDIT: just assuming num is int because one shouldn't compare floats for equality.Code:printf("%d",num);
Last edited by ZuK; 02-08-2006 at 04:06 AM.
Changing the code to print in hexadecimal isn't hard since binary have pretty good relationship to it; 2^4 == 16
So what needs to be done is changing the code so that it processes 4 bits at a time instead of 1 bit. This will give results in the range of 0-15 so I made another function which translates that into the char values '0' to '9' and 'A' to 'F'
PS: I am sorry for my use of magic numbers but I am a jerk and I don't feel like making the code look more messy than it already is.Code:#include <stdio.h> #include <stdlib.h> void dectohex (float num); char gethexchar (int n); int main (int argc, char** argv) { float num; if (argc >= 2) num = atof(argv[1]); else scanf("%f", &num); dectohex(num); return 0; } void dectohex (float num) { int int_part; unsigned int i, pos = sizeof(int)*8 - 4; const unsigned int int_msb = 0xF << (sizeof(int)*8 - 4); //Even better pyramid. int_part = num; num -= int_part; if (int_part < 0) { putc('-', stdout); int_part = -int_part; num = -num; } putc('0', stdout); putc('x', stdout); //puts() is a jerk and prints a newline for (i = 0; pos; i += 4, pos -= 4) { if (int_part & (int_msb >> i)) break; } for (; pos; pos -= 4) { putc(gethexchar(((int_part & (0xF << pos)) >> pos)), stdout); } putc(gethexchar(int_part & 0xF), stdout); if (num) putc('.', stdout); while (num) { num *= 16; putc(gethexchar((int) num & 0xF), stdout); num -= (int) num; } } char gethexchar (int n) { if (n > 9) return 'A' - 10 + n; return '0' + n; }
Last edited by OnionKnight; 02-08-2006 at 08:49 PM.