0

i'm trying to play around and learn c while making a ohms calculator. now i have a strange thing happening when the resistance(output) is 0.001 then it prints out 1000uOhm instead of 1 mOhm

        case 1:
            printf("what is your Voltage?\n");
            scanf("%Lf", &volt);
            printf("what is your Amperage?\n");
            scanf("%Lf", &ampere);
            output = volt / ampere;
            if (output >= 0.0000000001 && output < 0.000001) {
                printf("%0.0Lf %s\n", output * 1e6, "nOhm"); // Convert to nano
                printf("%0.9Lf %s\n", output, "Ohm"); // Display as is
            }
            if (output >= 0.000001 && output < 0.001) {
                printf("%0.0Lf %s\n", output * 1e6, "uOhm"); // Convert to micro
                printf("%0.9Lf %s\n", output, "Ohm"); // Display as is

            }
            if (output >= 0.001 && output < 1 ) {
                printf("%0.0Lf %s\n", output * 1e3, "mOhm"); // Convert to mili
                printf("%0.9Lf %s\n", output, "Ohm"); // Display as is
            }
            if (output > 1) {
                printf("%0.0Lf Ohm\n", output);
            }

            break;

full code https://github.com/toku1991/ElectronicsCalculator

if i change if (output >= 0.001 && output < 1 ) to if (output >= 0.0009 && output < 1 ) it works but i would like to know whats going wrong?

Tom
  • 1
  • 1
  • Does this help you understand? https://stackoverflow.com/questions/588004/is-floating-point-math-broken (PS: use lower case 'el' in the format specifier... `%lf`, not `%Lf`) – Fe2O3 Aug 26 '23 at 08:21
  • seems to be floating point error. try using double instead of float. Read this https://www.geeksforgeeks.org/c-float-and-double/ – Marko Taht Aug 26 '23 at 08:22
  • The linked duplicate explains why comparing a flot via `=` to anything often has unexpected results. Using `>=` has the same problem on the edge case. Your change to a smaller number would allow to compare `>` which is the work around, that you use `>=` is practically the same. – Yunnosch Aug 26 '23 at 08:44
  • Question closed... Check the `1e6` in the "nano" `printf()`... Probably wrong... And, think about using `if/else` to remove a whole swack of code... – Fe2O3 Aug 26 '23 at 08:45
  • thanks for quick answer but its already a long double sow i think i'm using the wrong specifier but my ide told me to use this one sow – Tom Aug 26 '23 at 08:45
  • Using `double` instead of `float` does NOT solve the floating point issue, it just often hides it better. Any proposal to "solve" that way should be treated with care, if not doubted or ignored. (Sorry Marko.) That you see `double` not helping supports this. And be glad that you learned this way, going away with the impression that using doubles helps would have confused you no end. – Yunnosch Aug 26 '23 at 08:48
  • "_...play around and learn c while..._" Don't forget your math lessons. If `amps = 0.0`, you've got **division by zero** causing your program to crash and burn... **Never** count on user input... (Checking the return value from `scanf()` is another missing bit of code.) – Fe2O3 Aug 26 '23 at 08:54
  • 1
    @Fe2O3 Thanks your first link gave me the solution and your tip helped me improve me code – Tom Aug 26 '23 at 09:01
  • Consider: `int a = 3; if( a < 1 ) /*stuff*/; else if( a < 2 ) /*otherstuff*/; else /*threeStuff*/;` You only need to test if less than. When a match is found, stuff gets done. In a situation like this, there is no need to test for a _range_... – Fe2O3 Aug 26 '23 at 09:05

0 Answers0