hello!

I got unexpected output (and have no idea how to get that output as in C 
program)
    
    
    $ nim -v
    Nim Compiler Version 2.0.8 [Linux: amd64]
    Compiled at 2024-07-03
    Copyright (c) 2006-2023 by Andreas Rumpf
    
    $ gcc --version
    gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
    
    
    Run
    
    
    #include <stdio.h>
    
    int main(void) {
      // fahr is 0, 20, ... 300
      
      float fahr, celsius;
      int low, up, step;
      
      low = 0;   // from 0
      up = 300;  // to 300
      step = 20; // with step 20
      
      printf("Fahrenheit   Celsius\n");
      
      for(fahr = low; fahr <= up; fahr = fahr + step){
        celsius = (5.0 / 9.0) * (fahr - 32.0);
        
        printf("%3.0f   %6.1f\n", fahr, celsius);
      }
      
      printf("done!\n");
      
      return 0;
    }
    
    
    Run

output
    
    
    ./program_c
    Fahrenheit   Celsius
      0    -17.8
     20     -6.7
     40      4.4
     60     15.6
     80     26.7
    100     37.8
    120     48.9
    140     60.0
    160     71.1
    180     82.2
    200     93.3
    220    104.4
    240    115.6
    260    126.7
    280    137.8
    300    148.9
    done!
    
    
    Run
    
    
    import std/math
    
    var
      fahr, celsius: float
      low, up, step: float
    
    low = 0.0   # from 0
    up = 300.0  # to 300
    step = 20.0 # with step 20
    
    echo "Fahrenheit   Celsius"
    
    while fahr <= up:
      celsius = round( (5.0 / 9.0) * (fahr - 32.0), 1)
      echo fahr, "   ", celsius
      fahr = fahr + step
    
    echo "done!"
    
    
    Run

output
    
    
    ./program_nim
    Fahrenheit   Celsius
    0.0   -17.8
    20.0   -6.7
    40.0   4.4
    60.0   15.6
    80.0   26.7
    100.0   37.8
    120.0   48.9
    140.0   60.0
    160.0   71.09999999999999
    180.0   82.2
    200.0   93.3
    220.0   104.4
    240.0   115.6
    260.0   126.7
    280.0   137.8
    300.0   148.9
    done!
    
    
    Run

71.1 =/= 71.09999999999999 so round(x, 1) is buggy? how to fix it? thank you

Reply via email to