Skip to content

Semantics of trunc, round and ceil #18

Open
@emilaxelsson

Description

@emilaxelsson

From the old trac:

The C functions trunc, round and ceil return 0 if the following conditions are satisfied:

  • their parameter is less than -32768 or greater than 32767
  • we want to store the result in a variable of type int8_t

Tested on: Fedora 15 x86
gcc (GCC) 4.6.0 20110530 (Red Hat 4.6.0-9)
Command line: gcc -W -Wall test.c -std=c99 -lm

A small test program:

#include <inttypes.h>
#include <stdio.h>
#include <math.h>

int main()
{
    float f = -33000.5;
    float origf = f;
    int8_t n;
    for (int i=0;i<1000;i++)
    {
        n = round(f);
        if (n==0)
            printf("%0.2f: %hd --- ", f, n);
        f += 1.0;
    }
    printf("\n");

    n = round(origf);
    printf("round(origf)=%hd, origf=%f\n", n, origf);
    n = round(-33000.5l);
    printf("round(-33000.5)=%hd\n", n);
    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions