adding modf pow and round
This commit is contained in:
28
srcs/ft_round.c
Normal file
28
srcs/ft_round.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "libft.h"
|
||||
|
||||
/**
|
||||
* round x to the nearest integer,
|
||||
* but round halfway cases away from zero, regardless of the current rounding direction
|
||||
*/
|
||||
double ft_round(double x)
|
||||
{
|
||||
// Handle special cases
|
||||
if (x != x)
|
||||
{ // Check for NaN (NaN is not equal to itself)
|
||||
return x; // Return NaN
|
||||
}
|
||||
if (x == (double)LLONG_MAX + 1.0 || x == (double)LLONG_MIN - 1.0)
|
||||
{
|
||||
return x; // Return ±Infinity or the original value if overflow would occur
|
||||
}
|
||||
|
||||
// Handle positive and negative numbers
|
||||
if (x >= 0)
|
||||
{
|
||||
return (double)(long long)(x + 0.5);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (double)(long long)(x - 0.5);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user