square root algorithm

# Thread: square root algorithm – 4831 days old

1. 0

## square root algorithm

Hi everyone,
In a hydraulic orifice flow , one has to compute the actual fluid flow from the differential pressure drop across the orifice plate according to the following equation:

Flow=k*sq.root(D.P)

I have an analog input coming into 16F877A proportional to the D.P.But to compute the actual flow by the 'SQR' operator is out of question since it gives a truncated integral result for 14-bit core devices as evident from the following:

With 12, and 14-bit core devices, Sqr returns an integer square root of a value, compatible with the BASIC Stamp syntax. Remember that most square roots have a fractional part that the compiler discards in doing its integer-only math. Therefore it computes the square root of 100 as 10 (correct), but the square root of 99 as 9 (the actual is close to 9.95). Example: -

VAR1 = SQR VAR2

or

PRINT DEC SQR 100 ' Display square root of 100 (10).
PRINT DEC SQR 99 ' Display of square root of 99 (9 due to truncation)

However, with the extra functionality, and more linear memory offered by the 16-bit core devices, full 32-bit floating point SQR is implemented.
Can anyone help with a functional square root algorithm for floating point variable in a 14-bit core device?

2. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

3. 0
You could use a lookup table instead of calculating every value anew - but this will depend on accuracy required and eprom space.

You could also calculate it the long way you did in school instead of using the SQR command.

4. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

5. 0
Hi Fanie!

You could use a lookup table instead of calculating every value anew - but this will depend on accuracy required and eprom space.
EEPROM may not be feasible due to space restrictions.

Can you think of any iterative method for square root extraction?
Regards
Shantanu

6. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

7. 0
Why don't you just swap to a 18f4520?

8. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

9. 0
Yes Tim,that option can always be taken in theory.But I would be constrained by the price difference between the two chips.Also,to be very frank,the 18-series I.Cs are not easily available in India.
Regards
Shantanu

10. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

11. 0
Just a thought ... if you can afford the space to make "DP" a Dword, then initially multiplying DP x 10,000 (assuming DP is not a large number to begin with) & taking the SQR will provide an answer that's a factor of 100 larger ... dividing by 100 restores 2 decimal places of accuracy if converted to floating point. Better still avoid floating point & factor the "x 100" into subsequent calculations as an integer. I've used this to save SQR code space even on 16 bits devices.

Hope this helps,

Rob R

12. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

13. 0
There is an example of how to do this here, when converted into Picbasic it looks like this:
Code:
```Device 16F877

Symbol Tol = 0.000005

Dim Value As Float
Dim Old_app As Float
Dim New_app As Float
Dim Result As Float

If Value < 0 Then

' Cannot find square root of negative number, give up!

Else
If Value = 0 Then
Result = 0
Else
Old_app = Value ' take value as first approximation
New_app = Old_app + Value / Old_app
New_app = New_app / 2

While Abs((New_app - Old_app) / New_app) > Tol
Old_app = New_app
New_app = Old_app + Value / Old_app
New_app = New_app / 2
Wend

Result = New_app
End If
End If```
It takes roughly two iterations for each digit to the left of the decimal point, but this can be improved upon by setting the initial value of Old_app to what you expect to be a typical result. For example, if the expected input range is from 100 to 10,000 then starting with a value of 30 reduces the maximum number of iterations from 10 to only 5. Unless execution speed is critical, this probably isn't worth spending too much time optimising.

14. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

15. 0
Hi Yakovlev & Nigel,
Great idea!!
I will try out & let you know at the earliest.
Regards
Shantanu

16. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

17. 0
I will try out & let you know at the earliest.
Don't bother, it doesn't work!

I have just run it through the simulator, and although it compiles OK the compiler chokes on the While statement - presumably because it is too complex. We need to break it down into two simpler bits, and it is easiest to do this in the loop and then check at the end rather than the beginning so the While/Wend becomes a Repeat/Until. This version works:
Code:
```Device 16F877

Symbol Tol = 0.000005

Dim Value As Float
Dim Old_app As Float
Dim New_app As Float
Dim Temp As Float
Dim Result As Float

If Value < 0 Then

' Cannot find square root of negative number, give up

Else
If Value = 0 Then
Result = 0
Else
Old_app = Value ' take value as first approximation

Repeat
Old_app = New_app
New_app = Old_app + Value / Old_app
New_app = New_app / 2
Temp = Abs((New_app - Old_app) / New_app)
Until  Temp < Tol

Result = New_app

End If
End If```
It is a shame that the compiler doesn't know its limitations and warn us that what we are trying to do won't work. Without the simulator, this would have taken ages to get working!

18. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

19. 0
Thanks Nigel for all your help!!
I had faced similar problems before while dealing with floats.But the compiler at least had warned me then. This is from a previous post:
I have written a data acquisition software where the BCD data from DS1307 RTC(Minute) is being converted to hex using the following code:

rtc_min_hex=((rtc_minute>>4)*10)+(rtc_minute & \$0F)

( rtc_min & rtc_min_hex are byte variables)

A few lines before this, a floating point multiplication is being
carried out to calculate the current from the register of a energy metering
chip(CS5460) & convert it into its actual engineering unit:

rms_volt=raw*(250.0/65535)

During compilation the following error message is coming:

"FLOAT type variables not supported in multi-part expressions.
i.e. More than 3 operands. Split the expression into smaller pieces!"

When I am "splitting" the expression in this way,

rtc_min_hex=((rtc_minute>>4)*10)
rtc_min_hex=rtc_min_hex+(rtc_minute & \$0F)

the compilation is being done successfully.
Also,when I am deleting the floating point calculation the compilation is successful.

Is this error message due to the floating point routines occupying a lot of system variables or something else?
We have to learn to live with these pitfalls.
Regards
Shantanu

20. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues