Floating point - please enlighten me!


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I have never used them, maybe they are OK.
    http://www.awce.com/compare.htm
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Floating point is a limitation of the PBP compiler. Nothing to do with PICs in general. You can use a floating point library and get by, but the question is do you really really need it? Most times, you will be using compile time fractions which can be broken down into simple integer operations.

    The most general work around is to find an equivalent fraction that can be represented in integer math. For example, if you need to do pi x d, you can do

    (d * 22)/7 to get a value in integers
    or
    (d*220)/7 to get a value with 1 decimal place. Only now, you need to place the decimal point while displaying the value.

    I use an utility called MISCEL from http://www.miscel.dk which allows you to compute integer equivalents of a fraction in various word sizes.

    The liabilities of Floating Point
    1 - Bulky code
    2 - Very time intensive computing A single operation can take approx 2-300mS
    3 - PBP does not support it

    Of course, even the long support needs you to migrate your code to an 18F series

  3. #3
    Join Date
    Nov 2010
    Posts
    8


    Did you find this post helpful? Yes | No

    Question More Floating Point Help

    Hello

    Trying to use some floating point.

    PIC16f886

    I have tried it several ways by no real luck

    I have an number that need to be mulitiplied by .2235 to get corrrect result

    Result can be rounded back to integer

    For example

    number = 250 usually a varriable but have tried it this way for testing

    I have tried

    number * .2235 ( won't compile)

    and

    number * (1000/4474) Compiles but result is zero

    Answer should be somewhere around 55.8

    I am not sure if the math is not working or I can not just see the number

    That is; once the number is computed it is sent out RS232, it would work best if it was a whole number between 0-255 at time of sending
    in this case 56 would be great.

    Not sure where the problem lies.

    Should the math be working ?

    Is so how do I round to a whole number ?

    thanks

    Mark




    Quote Originally Posted by mackrackit View Post
    I have never used them, maybe they are OK.
    http://www.awce.com/compare.htm

  4. #4
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default

    Try to use DIV32.
    Code:
    Dummy VAR WORD
    Number VAR BYTE
    Result VAR BYTE
    Dummy = Number * 1000
    Result=DIV32 4474

  5. #5
    Join Date
    Oct 2005
    Location
    Loveland CO USA
    Posts
    83


    Did you find this post helpful? Yes | No

    Default

    Not using a compiler, using a calculator.
    Input=250 and you want about 1/4 of that.
    250*.2235=55.875
    With out using a decimal point!
    250*223/1000=55.75 (I did not use 2235/10000)
    If the input will fit into a byte and 223 fits into a byte. Then (byte * byte)=word and then word/1000=55.
    If you need a input like 350 which is a word and/or you want to use 2235/10000 then you will need to do word*word and word**word to get two words or 32 bits. You might also look at word*/word to get 16 bits out of the middle of the 32 bits.

  6. #6
    Join Date
    Nov 2010
    Posts
    8


    Did you find this post helpful? Yes | No

    Thumbs down WOW ! Thats Smart

    Not using a compiler, using a calculator.
    Input=250 and you want about 1/4 of that.
    250*.2235=55.875
    With out using a decimal point!

    Yes, I always like the types of Stupid answer's one gets when asking a simple question.

    Number is a variable and constantly changing.

    As I said for example 250

    This is just part of a longer calculation. If it was as simple as just putting a number I really wanted, I think I might of though of that....

    I was just looking for the format to input numbers like .00044

    thanks

    Quote Originally Posted by ronsimpson View Post
    Not using a compiler, using a calculator.
    Input=250 and you want about 1/4 of that.
    250*.2235=55.875
    With out using a decimal point!
    250*223/1000=55.75 (I did not use 2235/10000)
    If the input will fit into a byte and 223 fits into a byte. Then (byte * byte)=word and then word/1000=55.
    If you need a input like 350 which is a word and/or you want to use 2235/10000 then you will need to do word*word and word**word to get two words or 32 bits. You might also look at word*/word to get 16 bits out of the middle of the 32 bits.

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    Gee, and you question SO looked like you needed to multiply a whole number by .2235 and the .2235 was where the trouble is. If you need .00044*.2235, thats a different problem. You are correct, your question was simplified.Where are you getting numbers like .00044 in your pic? how is that generated?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  8. #8
    Join Date
    Nov 2010
    Posts
    8


    Did you find this post helpful? Yes | No

    Question Just trying to learn how to use work around in PIC of Decimal values

    Quote Originally Posted by cncmachineguy View Post
    Gee, and you question SO looked like you needed to multiply a whole number by .2235 and the .2235 was where the trouble is. If you need .00044*.2235, thats a different problem. You are correct, your question was simplified.Where are you getting numbers like .00044 in your pic? how is that generated?
    Hello

    Thanks

    I am just trying to understand the work around format in PIC to work with floating point values, the systems I normally program have floating point at part of there normal stucture.

    I though from the orignal post that all you needed to do is use fractions in your formula and it would work but it does not seem to be so.

    For example If I needed to multiply some varriable by .234 or any decimal value to get an answer, how would this be done?

    In my case I am takeing a analog voltage and turning in into a tempature that is displayed on a screen.

    The temperature result does not need to be floating point

    But if I can not use floating point numbers to scale correctly the value could be off by 5+- degrees pretty quickly

    The true formula is

    result=adc10((adc10*.00034)+.2235) 'where adc10 in the pic 10 analog register

    To be within 1% of the true tempature

    Thanks

  9. #9
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mark30504 View Post
    Yes, I always like the types of Stupid answer's one gets when asking a simple question.
    Well, as was stated your question, ronsimpson did answer very good.

    A simple thank would be OK if you did no liked the time he spent for you.

    Anyway, PBP does not support FP directly. So tricks like the one suggested should be used.

    A byte is 0-255 in range and a word 0-65535, so none will store you values as is.

    Have a look at n-bit math here:

    http://www.picbasic.co.uk/forum/show...ght=n-bit+math

    or real Floating Point here:

    http://melabs.com/resources/fp.htm

    Ioannis

  10. #10
    Join Date
    Nov 2010
    Posts
    8


    Did you find this post helpful? Yes | No

    Thumbs up Thanks I will give it a try, have a few questions though.

    Quote Originally Posted by pedja089 View Post
    Try to use DIV32.
    Code:
    Dummy VAR WORD
    Number VAR BYTE
    Result VAR BYTE
    Dummy = Number * 1000
    Result=DIV32 4474
    Make sure I have this right.

    By defineing Result at a byte, this will round the result to byte lenght ?

    Lets say I need a decimal like .0004
    Would I need several lines of code or can I just use

    Dec1 VAR WORD
    Result VAR BYTE
    Something VAR WORD

    Dec1=1/2500 'this should be .0004 ' will this work ? or need DIV32
    Result = Something * Dec1 ' something being between 0 and 500

    Result is now rounded ?

    Do I alway need a DIV32 when a want a decimal result ?

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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