Page 1 of 1

Division by 10 easy?

Posted: Tue May 14, 2019 3:18 pm
by Jan Lichtenbelt
I have a simple counter from 1 to 20. But I need the counter diviede by 10. Counter is A_byte and the 10th part is A_real. The simulation works correctly. But the microchip has different ideas about this 'problem'. All numbers are correct, except A_byte=14 and A_real should be 1.4. The reality is, see picture
Counter_V1.jpg
Counter_V1.jpg (165.14 KiB) Viewed 4086 times
How is this possible?
Test_Counter_V1.fcfx
(9.13 KiB) Downloaded 245 times
Kind regards

Jan

Re: Division by 10 easy?

Posted: Tue May 14, 2019 3:44 pm
by Jan Lichtenbelt
In the meanwhile I found the solution. All real numbers have positive rounding errors, while 1.4 has been calculated with an negative error with results 1.399...... The 3 lift characters will result into 1.3 instead of 1.4

Kind regards

Jan

Re: Division by 10 easy?

Posted: Thu May 16, 2019 10:06 am
by Steve
Hi Jan,

If you just want to display the divided number rather than do any maths on it, then you could do something like this:

1) convert the integer to a string:

Code: Select all

value_as_string = ToString$(my_int_value)
2) display the first part of the string:

Code: Select all

Left$(value_as_string, Length$(value_as_string)-1)
3) display a "."


4) display the last digit:

Code: Select all

Right$(value_as_string, 1)
Regards,
Steve.

Re: Division by 10 easy?

Posted: Thu Mar 19, 2020 8:57 am
by AbhijitR
Hello! Jan

Many thanks for this post, some what life saving for me.
Jan Lichtenbelt wrote:In the meanwhile I found the solution. All real numbers have positive rounding errors, while 1.4 has been calculated with an negative error with results 1.399...... The 3 lift characters will result into 1.3 instead of 1.4
Any idea how to round off the answer 12.3 to 12 and 12.6 to 13, I think to round off to the nearest value before and after 0.5

Thank you again.

Regards
Abhi

Re: Division by 10 easy?

Posted: Thu Mar 19, 2020 11:00 am
by medelec35
AbhijitR wrote: Any idea how to round off the answer 12.3 to 12 and 12.6 to 13, I think to round off to the nearest value before and after 0.5
Hi Abhi,
I believe the round() function will do what you are after.
If you open a calculation box, then select fx, you can scroll down to round().

Re: Division by 10 easy?

Posted: Thu Mar 19, 2020 11:58 am
by AbhijitR
Hi! Martin
medelec35 wrote: If you open a calculation box, then select fx, you can scroll down to round().
No exaggeration, you are more or less WIKI for FC, thank you once again for that quick reply.

Regards
Abhi