Page 1 of 1

Float Calculation

Posted: Sun Jan 08, 2017 12:51 pm
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

Re: Float Calculation

Posted: Sun Jan 08, 2017 12:58 pm
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.

Re: Float Calculation

Posted: Sun Jan 08, 2017 5:59 pm
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

Re: Float Calculation

Posted: Sun Jan 29, 2017 6:35 pm
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

Re: Float Calculation

Posted: Tue Jan 31, 2017 9:03 am
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

Re: Float Calculation

Posted: Wed Mar 17, 2021 9:06 pm
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

Re: Float Calculation

Posted: Thu Mar 18, 2021 10:18 am
by Steve
George - you have already asked this question here:
viewtopic.php?f=68&t=22668