Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 130261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:55:12+00:00 2026-05-11T05:55:12+00:00

I’m getting unexpected results from the round() and roundf() functions in the math.h library.

  • 0

I’m getting unexpected results from the round() and roundf() functions in the math.h library. Here is the sample code:

#include <stdio.h> #include <math.h>  int main(void) {     float f;     double d;      /* Note man page says that roundf() returns a float        and round() returns a double */     f = roundf(1.2);     d = round(1.2);      printf('%f\n', f);     printf('%lf\n', d);      return 0; } 

When I complie and run the program I get:

gcc -lm round.c ./a.out 288.000000 524288.000000 

Wierd huh?

Update: Along with the answers I’ve confirmed that the code works properly on a newer complier. (I think %lf is not the correct printf specifier but that doesn’t affect the end result in this case.) I’ll need to figure out why may compiler is behaving this way because I have running code that uses round() and has been compiler on the same machine. I’ll update the post when I figure it out.

gcc -v Reading specs from /usr/lib/gcc-lib/i386-slackware-linux/2.95.3/specs gcc version 2.95.3 20010315 (release) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:55:13+00:00Added an answer on May 11, 2026 at 5:55 am

    You can fail if you compile the code without telling gcc to compile with C99 mode (-std=c99) and tell it not to know about ‘special builtin’ functions (using -fno-builtin). Then it assumes that your round/roundf function is defined as

    int round(); int roundf(); 

    Because in pre-C99 times there were no such functions yet, so it does not have a declaration and implicitly declares them then. And this will obviously fail, because the call-side treats the return value as an int, while the callee side (in the definition of those functions in the linked library) returns a float. I get these results for example:

    [js@HOST2 cpp]$ ./a.out 1065353216.000000 -1048576.000000 [js@HOST2 cpp]$ 

    Not that you think now that you could cast the return value to a float and be OK. Well, it’s worse. The return value is not even guaranteed to have anything to do with the float returned. The call-side reads from a place that it knows where integers are returned. But your compiler may return floats in another place (say, in a floating pointer register) from the callee side. The above could actually have done anything, including aborting the program because it behaves in an undefined manner.

    So, what can you do to make it work? Pass the compiler the std=c99 flag or use other ways to round (floor is one of them) which do not require those functions

    gcc -std=c99 test.c -lm 

    See the manpage of man 3 round. However, if you have a very old GCC – i’m not sure whether it supports enough of C99 to have that switch. Look into the feature test macros described in the manpage of round then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.