Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this will show that that conversion error for i32 and f32 is 64 and ~2.98023224e-8 respectively
- //(0.0000000298023223876953125 in normal notation, or about 3 ten millionths of a percent)
- #define RANGE 100000000
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <stdint.h>
- int get_random_i32(){
- union {
- struct {
- uint16_t l;
- uint16_t h;
- };
- int32_t i;
- } output;
- output.l=rand();
- output.h=rand();
- return output.i;
- }
- //multiplying by inv32 is equivalent to dividing by 0x7fffffff
- const float inv32=1.0f/0x7fffffff;
- int32_t convert_from_and_to(int32_t input){
- float from=(float)input*inv32;
- return from*2147483647; //to
- }
- int main(){
- time_t t;
- srand((unsigned)time(&t));
- int32_t mn=0,mx=0;
- for(int i=0; i<RANGE; ++i){
- int32_t src=get_random_i32();
- int32_t dst=convert_from_and_to(src);
- int32_t dif=dst-src;
- if(dif<mn) mn=dif;
- if(dif>mx) mx=dif;
- //printf("%11i -> %11i (%i)\n",src,dst, dif);
- }
- //given enough RANGE, this should end up being -64 and 64 respectively
- printf("min=%i, max=%i\n",mn,mx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement