square root algorithm


+ Reply to Thread
Results 1 to 10 of 10

Thread: square root algorithm4831 days old

  1. #1
    Prolific Poster shantanu@india's Avatar
    Join Date
    Feb 2007
    Posts
    1,705
    Thumbs Up
    Received: 7
    Given: 21
    Total Downloaded
    827.23 MB

    0 Not allowed!

    Default 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?

    Thanks in advance!!

  2. Attention
    ×

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  3. #2
    Fanatical Contributor fanie's Avatar
    Join Date
    Oct 2005
    Posts
    7,989
    Thumbs Up
    Received: 31
    Given: 15
    Total Downloaded
    434.52 MB

    0 Not allowed!

    Default

    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.
    Fanie

  4. Attention
    ×

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  5. #3
    Prolific Poster shantanu@india's Avatar
    Join Date
    Feb 2007
    Posts
    1,705
    Thumbs Up
    Received: 7
    Given: 21
    Total Downloaded
    827.23 MB

    0 Not allowed!

    Default

    Hi Fanie!
    Thanks for your suggestions.

    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  7. #4
    Fanatical Contributor Tim's Avatar
    Join Date
    Jan 2003
    Posts
    7,754
    Thumbs Up
    Received: 51
    Given: 96
    Total Downloaded
    1.33 GB

    0 Not allowed!

    Default

    Why don't you just swap to a 18f4520?
    Tim

  8. Attention
    ×

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  9. #5
    Prolific Poster shantanu@india's Avatar
    Join Date
    Feb 2007
    Posts
    1,705
    Thumbs Up
    Received: 7
    Given: 21
    Total Downloaded
    827.23 MB

    0 Not allowed!

    Default

    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  11. #6
    Junior Member Yakovlev's Avatar
    Join Date
    Feb 2004
    Posts
    43
    Thumbs Up
    Received: 0
    Given: 1
    Total Downloaded
    22.11 MB

    0 Not allowed!

    Default

    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  13. #7
    Nigel Worsley
    Guest Nigel Worsley's Avatar

    0 Not allowed!

    Default

    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  15. #8
    Prolific Poster shantanu@india's Avatar
    Join Date
    Feb 2007
    Posts
    1,705
    Thumbs Up
    Received: 7
    Given: 21
    Total Downloaded
    827.23 MB

    0 Not allowed!

    Default

    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  17. #9
    Nigel Worsley
    Guest Nigel Worsley's Avatar

    0 Not allowed!

    Default

    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  19. #10
    Prolific Poster shantanu@india's Avatar
    Join Date
    Feb 2007
    Posts
    1,705
    Thumbs Up
    Received: 7
    Given: 21
    Total Downloaded
    827.23 MB

    0 Not allowed!

    Default

    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Comparing two square waves
    By SimonTempler in forum Proton Plus Compiler v3
    Replies: 7
    Last Post: 21st June 2011, 16:17
  2. [SOLVED !] duty factor algorithm
    By John7 in forum The Lounge
    Replies: 1
    Last Post: 12th March 2006, 00:33
  3. MD5 algorithm.
    By speeder in forum The Lounge
    Replies: 4
    Last Post: 12th August 2005, 12:10
  4. [SOLVED !] MD5 algorithm.
    By speeder in forum Proton Plus Compiler v3
    Replies: 0
    Last Post: 11th August 2005, 16:12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts