How to raise the float

Asked 1 years ago, Updated 1 years ago, 104 views

We need a float raise function that runs like the code below math.h has ceil() and floor() but not round(). Is it in another C++ library or not at all?

float round(float);

round(0.1) = 0
round(-0.1) = 0
round(-0.9) = -1

c++ floating-point rounding

2022-09-22 11:38

1 Answers

Considering that there is only ceil() and floor(), it seems to be a C++98 environment. C++98 does not have a round() function, so you have to make it yourself. The code is as follows

float round(float d)
{
  return floor(d + 0.5);
}


2022-09-22 11:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.