Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
19 years ago

round function

Hi,

I want to round a floating point number before I cast it to an int.

So, round 3.6 to 4.0

I try using the function round and the compiler buys it but when I do

round(3.6) or round(3.6,0)

I get 0

What gives?

Thanks,

Mike

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    how about:

    float x = 3.6;

    int y;

    y = (int)(x + 0.5); // (3.6+0.5 = 4.1 which will get dragged down to 4)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Yeah, I *guess*.

    Not quite as elegant as round but truthfully it will probably execute much quicker than any round function anyway.

    It bothers me that round seems to be defined but doesn't really work the way I expect it to but after writing code for Nios for a year I should not be surprised.

    Thank you for the response BadOmen.

    Mike