Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
Rounding float to nearest integer in C

I might have a noob question here, but searching the site hasn't yelded anything. I'm learning to program in C and I'm trying to build a function from scratch that rounds floats to the nearest integer, without using math.h. Here's my code:

void main()
{
    float b;
    for(b = 0; b <= 2; b = b + 0.1)
    {
        printf("%f    ", b);
        printf("%i    ", (int)b);
        printf("%f    ", b - (int)b);
        printf("Nearest: ");
        if((b - (int)b)<0.5)
            printf("%i    ", (int)b);
        else
            printf("%i    ", (int)b + 1);
        printf("Function: %i    ", round_near(b));
        printf("\n");
    }
    getchar();
}

int round_near(float b)
{
    if((b - (int)b)<0.5)
        return(int)b;
    else
        return (int)b + 1;
}

My results looks like this:

enter image description here

Some of the code is superfluous and was just meant to see some of the individual steps of my function. What gives? Are there some shenanigans with float type variables I'm not aware of?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You don't have a prototype for int round_near(float b), so you're relying on implicit declarations.

Try adding this to your code.

int round_near (float b); // Prototype

int main(void) // Nitpick: main returns an int!

Using implicit declarations for round_near(b), b is being promoted to a double. But the definition assumes it's a float, which has a different binary layout, so you get crazy random results.

You should make sure your code compiles without any warnings to avoid this sort of stuff. The only reason implicit declaration is in the language is for backwards compatibility, but every compiler for the last decade or two warns you that it's bad on compile.

over 4 years ago · Santiago Trujillo Report

0

When I tried to compile this under gcc I got the following error:

/tmp/x1.c:23: error: conflicting types for ‘round_near’
/tmp/x1.c:23: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
/tmp/x1.c:16: error: previous implicit declaration of ‘round_near’ was here

The funny results you're getting is because your compiler didn't know the definition of round_near at the time it was first encountered and assumed it was int round_near(). So this resulted in undefined behavior.

If you either move round_near above main or put in a declaration above main you should get the expected results.

over 4 years ago · Santiago Trujillo Report

0

@QuestionC well answered OP's immediate problem: implied function signature of int round_near(...) is incompatible with int round_near(float b) and call of round_near(b) which passes b as a double.

Simple solution: prototype the function.

Some issues about the round_near()

  1. Casting to int severely narrows the legitimate range. Better to use long long.

  2. General incorrect functionality with negative numbers. @Eugene Sh. Code should test for sign.

Below is a solution that takes advantage of the range of long long as it is usually greater than the continuous range of integers a float can represent exactly. Alternatively OP can replace my_roundf() with round_near() and use this code for testing. round_near() fails about 40% of the time.

#include <limits.h>
#include <stdio.h>

float my_roundf(float x) {
  // Large `float`s typically have no fractional portion to round
  if (x > LLONG_MAX / 2) return x;
  if (x < LLONG_MIN / 2) return x;
  return x > 0 ? (long long) (x + 0.5f) : (long long) (x - 0.5f);
}

float rand_float(void) {
  union {
    unsigned char uc[sizeof(float)];
    float f;
  } u;
  do {
    unsigned i;
    for (i = 0; i < sizeof(float); i++) {
      u.uc[i] = rand();
    }
  } while (u.f != u.f);  // re-do if NaN encountered
  return u.f;
}

void my_roundf_test(void) {
  unsigned n = 100000;
  while (n-- > 0) {
    float x = rand_float();
    float ymath = roundf(x);
    // float ymy = round_near(x);
    float ymy = my_roundf(x);
    // Exact half-way cases may fail
    if (ymath != ymy) {
      printf("x:% .9e math:% .9e my:% .9e\n", x, ymath, ymy);
    }
  }
}

Note: There are exact half-way cases per various floating point rounding modes, negative zero, etc. to consider for a complete answer. But leave that for another day.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!