0

So I tried write a code to convert kmph to m/s And wanted to validate using Assert.

Here is the piece of code I wrote. The value is correct when tried comparing with print statements but assert gives an error

#include <stdio.h>
#include <assert.h>


float convert(float num);

int main()
    {
        float kmph;
         printf("Enter Velocity in Kmph\n");
        scanf("%f", &kmph);

        printf("Velocity in m/s is %f\n", convert(kmph));
        assert(convert(1) == 0.277778);
        assert(convert(18) == 5.0);
        return 0;
    }

     float convert(float num)
        {
           float mps;
           mps = num * 0.277778;

           return mps;

          }

error description:

   Enter Velocity in Kmph
   1
  Velocity in m/s is 0.277778
   a.out: program4.c:14: main: Assertion `convert(1) == 0.277778' failed.
    Aborted (core dumped)
  • Duplicate of [why if(a==2.3) evaluates false when float a=2.3](https://stackoverflow.com/questions/39616432/why-ifa-2-3-evaluates-false-when-float-a-2-3) – Guy Incognito Oct 08 '20 at 14:37
  • Does this answer your question? [Floating point equality and tolerances](https://stackoverflow.com/questions/17404513/floating-point-equality-and-tolerances) –  Oct 08 '20 at 14:39

0 Answers0