Page 1 of 1
Problem in the round(x) Function
Posted: Sat Mar 14, 2020 4:57 pm
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 287 times
- Avr.rar
- (19.76 KiB) Downloaded 244 times
Thanks
Re: Problem in the round(x) Function
Posted: Sat Mar 14, 2020 5:09 pm
by kersing
Please describe what your results are and what you expect to see.
Re: Problem in the round(x) Function
Posted: Sun Mar 15, 2020 3:22 pm
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
Mistake in the round(x) Function
Posted: Tue Mar 17, 2020 12:35 pm
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 235 times
- Avr.rar
- (19.76 KiB) Downloaded 233 times
Re: Problem in the round(x) Function
Posted: Wed Mar 18, 2020 12:33 pm
by RABO2320
No one to answer?
Re: Problem in the round(x) Function
Posted: Wed Mar 18, 2020 1:37 pm
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.
Re: Problem in the round(x) Function
Posted: Wed Mar 18, 2020 1:53 pm
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?
Re: Problem in the round(x) Function
Posted: Wed Mar 18, 2020 5:31 pm
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

.
Re: Problem in the round(x) Function
Posted: Tue Mar 31, 2020 9:29 pm
by Benj
Hello,
This problem should now be fixed with the latest updates. Help -> Check for updates.