Page 1 of 1

fround command pic 24fFV

Posted: Fri Mar 15, 2024 1:09 am
by Roy
Sir I am trying to use the fround command to limit a floating variable to 2 decimal places. It appears not to work in the simulation.

EMA_APHIA = 0.90

EMA_APHIA = fround (EMA_APHIA,2)

when I run the simulation I get 0.899999976------------lots more.
Does this command work in the simulation or only once complied?
While my program is not yet complete as I test each part as I go I have attached the flowcode file MAP_4CH_24FV_master_slave.fcfx
I have read the articles on the forum that tend to indicate the command does not work on 16bit PIC chips.
Can someone please enlighten me as to what I am doing wrong.
Thanks

Re: fround command pic 24fFV

Posted: Fri Mar 15, 2024 9:35 am
by mnf
One way to do this is to multiply the value by 100 (for 2 decimal places) and assign to an integer...
Note that not all numbers can be represented exactly by fp variables.

Can I also suggest an upgrade to v10 it's free and v4 is now very old

Martin

Re: fround command pic 24fFV

Posted: Fri Mar 15, 2024 10:20 am
by kersing
Please keep in mind computers work in ones and zeros, not floating point numbers. As a result some floating point numbers can not be represented exactly and are a close approximation. Check https://floating-point-gui.de/ for an easy explanation or https://ece.uwaterloo.ca/~dwharder/Num ... /paper.pdf for the heavy theory.

Re: fround command pic 24fFV

Posted: Fri Mar 15, 2024 8:11 pm
by Roy
Thanks for your comments . I am using version V8.
I would like to use the value in an EMA calculation (1-alhpia) as part of a filter.
It seems to work in my 8 bit version of the program.

Re: fround command pic 24fFV

Posted: Fri Mar 15, 2024 8:17 pm
by kersing
Calculations with the numbers should be fine, it is just displaying for us humans that doesn’t work as expected.