I am having an issue using the abs() function with float values. It worked perfect when using ints but when using float it always just outputs 1, even when it should be e.g 3.1 - 1.8 was coming out as 1. I am using arrays to get the values, just unsure as whats going on, I tried using fabs and changing from float to double but no difference.
x_distance = abs(x_coord[0] - x_coord[1]);
D:/Data D drive/University/Year_2/EEEE 2063/CourseWork2/test/main.c:33:18:
CodePudding user response:
abs
is for int
arguments. Using it with a floating-point argument truncates the value to an integer (or overflows).
Use fabsf
, fabs
, or fabsl
for float
, double
, or long double
, respectively. These are declared in <math.h>
.
Alternatively, include <tgmath.h>
(type-generic math) and use fabs
with any floating-point type.
CodePudding user response:
I just realised I didnt include the math.h library as I thought it was defined inside the stdlib.h. Thanks for your help everyone