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):
Thanks
Problem in the round(x) Function
Moderator: Benj
-
- 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
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
― C.S. Lewis
-
- 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
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
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
-
- 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
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):
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):
- 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
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.
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.
- 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
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.
Maybe the Floor function would do more as you expect?
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))
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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
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 .
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 .
- 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
Hello,
This problem should now be fixed with the latest updates. Help -> Check for updates.
This problem should now be fixed with the latest updates. Help -> Check for updates.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel