Problem in the round(x) Function

Moderator: Benj

Post Reply
RABO2320
Posts: 41
Joined: Sun Mar 01, 2020 1:39 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Problem in the round(x) Function

Post by RABO2320 »

Hi
I've encountered a problem with the round (x) function.
The function after compiling on the chip does not work properly on the negative part of the numbers.
This bug exists in both AVR and PIC compilers.

Examples of both AVR and PIC (with Proteus file):
Pic.rar
(25.45 KiB) Downloaded 187 times
Avr.rar
(19.76 KiB) Downloaded 173 times
Thanks

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Problem in the round(x) Function

Post by kersing »

Please describe what your results are and what you expect to see.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

RABO2320
Posts: 41
Joined: Sun Mar 01, 2020 1:39 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Re: Problem in the round(x) Function

Post by RABO2320 »

Results obtained:

round(-0.5) = 0
or
round(-1) = 0
or
round(-1.3) = 0
and
round(-1.7) = -1

These results are wrong on the chip but in Flowcode the results are correct

RABO2320
Posts: 41
Joined: Sun Mar 01, 2020 1:39 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Mistake in the round(x) Function

Post by RABO2320 »

Hi
I've encountered a problem with the round (x) function.
The function after compiling on the chip does not work properly on the negative part of the numbers.
This bug exists in both AVR and PIC compilers.
Results obtained:

round(-0.5) = 0
or
round(-1) = 0
or
round(-1.3) = 0
and
round(-1.7) = -1

These results are wrong on the chip but in Flowcode the results are correct.

Examples of both AVR and PIC (with Proteus file):
Pic.rar
(25.45 KiB) Downloaded 172 times
Avr.rar
(19.76 KiB) Downloaded 162 times

RABO2320
Posts: 41
Joined: Sun Mar 01, 2020 1:39 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Re: Problem in the round(x) Function

Post by RABO2320 »

No one to answer?

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Problem in the round(x) Function

Post by LeighM »

What is your expected result?
Looks like we simply add 0.5 (effectively rounding to nearest integer, for positive numbers at least.
Do you want rounding up, down, or towards zero?
If you have a project that expects a particular outcome you might need to create your own macro/function to do this.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Problem in the round(x) Function

Post by Benj »

Yes it looks like we round up.

2.0 = 2
1.5 = 2
1.0 = 1
0.5 = 1
0.0 = 0
-0.5 = 0
-1.0 = 0
-1.5 = 1
-2.0 = 1
-2.5 = 2

Here is the code.

Code: Select all

#define flt_round(f) ((long)((f) + 0.5))
Maybe the Floor function would do more as you expect?

RABO2320
Posts: 41
Joined: Sun Mar 01, 2020 1:39 pm
Has thanked: 1 time
Been thanked: 2 times
Contact:

Re: Problem in the round(x) Function

Post by RABO2320 »

Hi benj
The round (x) function must be the result of negative numbers such as positive numbers:
for example:
1.5 = 2
1.0 = 1
0.5 = 1
0.0 = 0
-0.5 = -1
-1.0 = -1
-1.5 = -2
-2.0 = -2
-2.5 = -3

But the floor function:
1.2 = 1
1.5 = 1
1.8 = 1
0.5 = 0
0.0 = 0
-0.5 = 0
-1.0 = -1
-1.5 = -1
-1.8 = -1
-2.5 = -2

The Flowcode works correctly but does not work on the chip after compiling :!: .

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Problem in the round(x) Function

Post by Benj »

Hello,

This problem should now be fixed with the latest updates. Help -> Check for updates.

Post Reply