| 1 | /* |
|---|
| 2 | * Small script to test whether or not multiplying an integer with a |
|---|
| 3 | * floating point number and back changes the integer |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #include <math.h> |
|---|
| 7 | #include <stdio.h> |
|---|
| 8 | #include <stdlib.h> |
|---|
| 9 | #include <time.h> |
|---|
| 10 | |
|---|
| 11 | main(){ |
|---|
| 12 | |
|---|
| 13 | // Variables to count the iterations. Loop over all integers from |
|---|
| 14 | // i to max_i. |
|---|
| 15 | int i = 0; |
|---|
| 16 | int max_i = 200000; |
|---|
| 17 | |
|---|
| 18 | // How many floats should be tested per integer. |
|---|
| 19 | int number_of_floats = 100000; |
|---|
| 20 | // The range of the floating point numbers. |
|---|
| 21 | float min_float = 0.00001; |
|---|
| 22 | float max_float = 100000.0; |
|---|
| 23 | float range = max_float - min_float; |
|---|
| 24 | // Once again because small numbers might cause more problems. |
|---|
| 25 | float small_min_float = 0.00001; |
|---|
| 26 | float small_max_float = 1.0; |
|---|
| 27 | float small_range = small_max_float - small_min_float; |
|---|
| 28 | |
|---|
| 29 | // Keep count. |
|---|
| 30 | long absolute_error_count = 0; |
|---|
| 31 | long absolute_sample_count = 0; |
|---|
| 32 | |
|---|
| 33 | // Generate a random number seed based on the time to get different results every time. |
|---|
| 34 | srand(time(NULL)); |
|---|
| 35 | |
|---|
| 36 | // Loop over all integer values. |
|---|
| 37 | while (i<max_i){ |
|---|
| 38 | |
|---|
| 39 | // Only print all 1000 values. |
|---|
| 40 | if (i%1000 == 0){ |
|---|
| 41 | printf("Current Integer: %i of %i\n", i, max_i); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | int current_errors = 0; |
|---|
| 45 | int current_count = 0; |
|---|
| 46 | |
|---|
| 47 | // Loop over random floating point values for every integer. |
|---|
| 48 | int j = 1; |
|---|
| 49 | while (j < number_of_floats){ |
|---|
| 50 | // Get the random float normalized between 0 and 1 and scale |
|---|
| 51 | // accordingly. |
|---|
| 52 | float random_float = (float)rand()/(float)RAND_MAX; |
|---|
| 53 | random_float = min_float + random_float * range; |
|---|
| 54 | // Convert integer to floating point. |
|---|
| 55 | float float_i = i * random_float; |
|---|
| 56 | float_i /= random_float; |
|---|
| 57 | // Round to avoid errors. |
|---|
| 58 | float_i = round(float_i); |
|---|
| 59 | // Convert back to integers. |
|---|
| 60 | int int_i = (int) float_i; |
|---|
| 61 | // Check if anything changed. |
|---|
| 62 | if (int_i != i){ |
|---|
| 63 | absolute_error_count += 1; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | // Again for the very small numbers. |
|---|
| 67 | random_float = (float)rand()/(float)RAND_MAX; |
|---|
| 68 | random_float = small_min_float + random_float * small_range; |
|---|
| 69 | // Convert integer to floating point. |
|---|
| 70 | float_i = i * random_float; |
|---|
| 71 | float_i /= random_float; |
|---|
| 72 | // Round to avoid errors. |
|---|
| 73 | float_i = round(float_i); |
|---|
| 74 | // Convert back to integers. |
|---|
| 75 | int_i = (int) float_i; |
|---|
| 76 | // Check if anything changed. |
|---|
| 77 | if (int_i != i){ |
|---|
| 78 | absolute_error_count += 1; |
|---|
| 79 | } |
|---|
| 80 | absolute_sample_count += 2; |
|---|
| 81 | |
|---|
| 82 | // Increment. |
|---|
| 83 | j += 1; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | i += 1; |
|---|
| 87 | } |
|---|
| 88 | double relative = ((double) absolute_error_count) / ((double) absolute_sample_count); |
|---|
| 89 | printf("\n\n===============\nFinal result:\n\tSamples: %li\nErrors: %li (%.3f %%)\n\n", absolute_sample_count, absolute_error_count, relative); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|