Float Calculation

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Float Calculation

Post by sysprofessional »

HI,
How to add decimal point in a float value,on required position.for example

I have a int value X=123456, and i want to add a decimal point on 3rd or 4th digit position,123.456,or 1234.56.

After converting it to float ,Y = int2float (X), Now Y = 123456.0

Here if i got dp on third position i need to divided it by 1000. Y= Y/1000.0 , now Y = 123.45600128173828

It is not accurate result .

i want to use this value in many calculations in program.I am using PIC18F14K22

I just move dp on any position where i want .

Best Regards

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: Float Calculation

Post by kersing »

Computers work with a binary representation of numbers. This is also the case for floating point numbers. The issue you are seeing is caused by the limitations of this representation. Check this Oracle write-up if you are interrested in the details.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Float Calculation

Post by medelec35 »

Don't know if this will be of interest?
You specify a Value & Decimal position, then results of whole and decimal are contained within separate variables .
Eg if Value = 123456 and decimal position = 3
Then Whole = 123 & Decimal = 456
At least then you can use that for calculations and sending to LCD etc.
Does not involve floats due to float results.

Not tried on hardware.

Martin
Attachments
Decimal Place.fcfx
(7.33 KiB) Downloaded 521 times
Martin

User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Re: Float Calculation

Post by sysprofessional »

Thnaks medelec35,

I am facing two issue at the end of program.one is calculation and 2nd is about port status during reset state.
In my program MCU get few parameters from sensors and some parameters feeded by user ,that is always in decimal value(float), store these parameters to EEPROM (after converting to integer) in case of power fail. Calculate all these values and display result on 7 segment display.

I have almost done all my job without extracting whole part and and decimal part from float ,Extraction method little confusing me ,i did not try it
, because it can increase the code size,PIC18f14k22 has 16 Kb Rom,and i have already almost to close it.

DP_F = int2float (Dp)
DP_F = pow (10, DP_F)
Tmp_F = int2float (Counter) // counter value updated by encoder
Tmp_F = (Tmp_F * SCL_F) // SCL_F is a float value ,feeded by user
Tmp_F = Tmp_Float * DP_F
Total = float2int (Tmp_F)

This calculation is working fine ,the only issue is that last digit is rounded every time ,for example if actual result is 0.5678, and dp=3 , then display result will be 0.568 .Or if dp = 2 ,the display result will be 0.57,
It is acceptable when dp > 0, But if Dp = 0 , result will be 1 ,that is the problem

I am not using round command in code, then why rounded automatically ,(rounded only on real hardware but not in Flowcode.)
Please suggest how can i remove rounded condition in calculation.

One scenario in mind , if SCL_F = 0.567
SCL = SCL_F * 1000
Total = (Counter * SCL) / 1000

But "Total" variable can overflow in this method .
___________________________________________

2nd issue is about port status during hard or soft reset. PIC18F14K22

i am using " reset; " command after all data stored into EEPROM.
I noticed that PortB.7 goes high during reset, and also goes high on power fail. (yes only B.7 Not whole portB)
while i have configured this bit as zero before, using output icon.
i have seen datasheet but could not find anything about this, Please guide me.\\

I want portB bit 7 low during reset .

Best regards
Tahir

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Float Calculation

Post by medelec35 »

Hi Tahir,
With the second issue:
In General when a microcontroller is first powered up, all the pins start of as input by default.
As the input will be a high resistance, then the voltage as the pins will then be determined by any component connected to them.
So you could add a 2K2 to 4K7 pull-down resistor on B.7 then it should stay low.

Will see if I get a chance to look at the calculation part.


Martin
Martin

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Float Calculation

Post by George_B »

Hello to everyone !

I know this is an old post but lets take it a little bit further.



In modbus, sometimes, we need to obtain float variables to describe a value.

As i have understand so far there will be need to send two integer variables in order to describe this float variable.



My question is, if i have a float variable = 24.256, how it is possible to split that into two integers (whole part and the real part i suppose)?

Thank in advance !

George

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Float Calculation

Post by Steve »

George - you have already asked this question here:
viewtopic.php?f=68&t=22668

Post Reply