Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Float vs Decimal

  1. #1
    AUGI Addict
    Join Date
    2006-04
    Location
    (getpoint "Anywhere on the Enter Key =>")
    Posts
    1,160
    Login to Give a bone
    0

    Default Float vs Decimal

    Hi ALL,

    Can anyone explain what are the difference of two statements below:
    1> (float (/ 120 2))
    2> (/ 120 2.0)

    I know they perform the same result and the first one always asks for more key punches while the last one saves half a second.

    Thanks in advance.

  2. #2
    I could stop if I wanted to
    Join Date
    2003-12
    Location
    Pittsburgh, PA
    Posts
    355
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    1> float creates a real from any number; useful if you only have integers and need a real.
    2> a real is returned since one of the operands is a real

  3. #3
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    Hi,

    (/ 10 3) returns 3
    (float (/ 10 3)) returns 3.0
    (/ 10 (float 3)) returns 3.3333
    (/ 10 3.0) returns 3.3333

  4. #4
    AUGI Addict
    Join Date
    2006-04
    Location
    (getpoint "Anywhere on the Enter Key =>")
    Posts
    1,160
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    Quote Originally Posted by lpseifert View Post
    1> float creates a real from any number; useful if you only have integers and need a real.
    2> a real is returned since one of the operands is a real
    Thanks.
    Yes, certainly I need a real number as result especially when the code doesn't know if the selection returns a integer or a real number.
    I know both statements having the same answer.

    My question are:
    1) what is the difference in LISP application.
    2) what are the advantages & disadvantages when they are applied.

  5. #5
    The Silent Type RobertB's Avatar
    Join Date
    2000-01
    Location
    Seattle WA USA
    Posts
    5,859
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    Quote Originally Posted by BoKirra View Post
    I know both statements having the same answer.

    My question are:
    1) what is the difference in LISP application.
    2) what are the advantages & disadvantages when they are applied.
    They do NOT return the same answer. Your testing method is flawed.

    $ (/ 120 7)
    17
    _$ (float (/ 120 7))
    17.0
    _$ (/ 120.0 7)
    17.1429

    • The first example is dividing two integers which will return an integer result.
    • The second example converts the result of the expression to a real. Note that in the case of the original expression, the result was an integer so the conversion to a real does not magically "fix" the integer result.
    • The third example shows the if the arguments given the operator include at least one real, a real is the result.


    All of which shows that data must be input, or corrected to the correct data type, before it is used in further expressions.
    R. Robert Bell
    Design Technology Manager
    Stantec
    Opinions expressed are mine alone and do not reflect the views of Stantec.

  6. #6
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    So to answer your question about benefits:
    • The (/ 10 3.0) method is less typing work, if you already know the value of at least one of the operands.
    • If you don't know any of their values, you use (float ...) to force them into a real number.
    E.g. Say you have two variables a and b with some number value assigned (you don't know if they're integers of reals). So you do the following (/ (float a) (float b)), you could of course have done (/ (float a) b) or (/ a (float b)) - which gives the same result (less typing).

  7. #7
    AUGI Addict
    Join Date
    2006-04
    Location
    (getpoint "Anywhere on the Enter Key =>")
    Posts
    1,160
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    Quote Originally Posted by irneb View Post
    So to answer your question about benefits:
    • The (/ 10 3.0) method is less typing work, if you already know the value of at least one of the operands.
    • If you don't know any of their values, you use (float ...) to force them into a real number.
    E.g. Say you have two variables a and b with some number value assigned (you don't know if they're integers of reals). So you do the following (/ (float a) (float b)), you could of course have done (/ (float a) b) or (/ a (float b)) - which gives the same result (less typing).
    Thanks for all your helps, irneb & Robert.

  8. #8
    AUGI Addict
    Join Date
    2006-04
    Location
    (getpoint "Anywhere on the Enter Key =>")
    Posts
    1,160
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    Quote Originally Posted by irneb View Post
    So to answer your question about benefits:
    • The (/ 10 3.0) method is less typing work, if you already know the value of at least one of the operands.
    • If you don't know any of their values, you use (float ...) to force them into a real number.
    E.g. Say you have two variables a and b with some number value assigned (you don't know if they're integers of reals). So you do the following (/ (float a) (float b)), you could of course have done (/ (float a) b) or (/ a (float b)) - which gives the same result (less typing).
    Sorry, I need to make sure what I've learnt is right.
    The following is an example from my another thread:
    I now added "float" at front of sHigh, sWidth & sThick local variables, highlighted in red.
    Are they correct?
    Thanks.

    Code:
      ;---------------------------------------------------------------------------------;
    (defun DTR (A) (* pi (/ A 180.0)))
      ;---------------------------------------------------------------------------------;
    (defun c:TEST
                   (/ IPt sHigh sWidth sThick PT1 PT2 PT3 PT4 PT5 PT6 PT7)
      (setvar "osmode" 111)
      (setq IPt (getpoint "\nselect Insert Point: "))
      (setq sHigh (getdist "\nEnter Section Height: "))
      (setq sWidth (getdist "\nEnter Section Width: "))
      (setq sThick (getdist "\nEnter Section Thickness: "))
      (setq
        PT1 (polar IPt (DTR 90) (float sHigh))
        PT2 (polar PT1 (DTR 0) (float sWidth))
        PT3 (polar IPt (DTR 0) (float sWidth))
        PT4 (polar PT3 (DTR 180) (float sThick))
        PT5 (polar PT4 (DTR 90) (- (float sHigh) (float sThick)))
        PT6 (polar PT5 (DTR 180) (- (float sWidth) (* sThick 2.0)))
        PT7 (polar IPt (DTR 0) (float sThick))
      ) ;end setq
      (command "pline" IPt PT1 PT2 PT3 PT4 PT5 PT6 PT7 IPt "")
      (setvar "osmode" 695)
      (princ)
    )

  9. #9
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    That should work, I think you've got the gist. But you're actually doing more than necessary in one or two lines. See my comments below in green.
    Code:
      ;---------------------------------------------------------------------------------;
    (defun DTR (A) (* pi (/ A 180.0))) ;The result here is already converted to float because of 180.0 - so you're correct in not converting to float below
      ;---------------------------------------------------------------------------------;
    (defun c:TEST
                   (/ IPt sHigh sWidth sThick PT1 PT2 PT3 PT4 PT5 PT6 PT7)
      (setvar "osmode" 111)
      (setq IPt (getpoint "\nselect Insert Point: "))
      (setq sHigh (getdist "\nEnter Section Height: ")) ;getdist will always return a float (real)
      (setq sWidth (getdist "\nEnter Section Width: ")) ;dito
      (setq sThick (getdist "\nEnter Section Thickness: ")) ;dito
      (setq
        PT1 (polar IPt (DTR 90) (float sHigh))
        PT2 (polar PT1 (DTR 0) (float sWidth))
        PT3 (polar IPt (DTR 0) (float sWidth))
        PT4 (polar PT3 (DTR 180) (float sThick))
        PT5 (polar PT4 (DTR 90) (float (- sHigh sThick))) ;subtract has no effect on int/float so do the float afterwards on (- 's result
        PT6 (polar PT5 (DTR 180) (- sWidth (* sThick 2.0)))) ;result of subtract is already forced to float because of (* sThick 2.0)
        PT7 (polar IPt (DTR 0) (float sThick))
      ) ;end setq
      (command "pline" IPt PT1 PT2 PT3 PT4 PT5 PT6 PT7 IPt "")
      (setvar "osmode" 695)
      (princ)
    )
    In this case it's not necessary to force a conversion to float as all the values are always obtained through getpoint / getdist. These functions are guaranteed to return a float (or real) value. Even when that value's a whole number it doesn't get returned as an integer.

  10. #10
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Float vs Decimal

    BTW, from the developer help about getdist:
    "Return Values
    A real number. If a 3D point is provided, the returned value is a 3D distance. However, setting the 64 bit of the initget function instructs getdist to ignore the Z component of 3D points and to return a 2D distance.
    "

    So you may have to use (initget 64) before each getdist to ensure a 2D distance is given, depending on your requirements of course.

Page 1 of 2 12 LastLast

Similar Threads

  1. Calculation of float station
    By civil3d.wishlist1941 in forum Civil 3D Wish List
    Replies: 0
    Last Post: 2010-10-07, 01:24 AM
  2. Dyn Block annotations float around
    By jaroguy in forum ACA General
    Replies: 5
    Last Post: 2007-12-03, 10:33 PM
  3. Fraction to Decimal
    By BCrouse in forum AutoCAD General
    Replies: 4
    Last Post: 2005-10-13, 12:13 PM
  4. Decimal Rounding Up
    By gjadams in forum AutoLISP
    Replies: 4
    Last Post: 2005-03-15, 02:46 PM
  5. Chairs Don't Float
    By mailtomac1860 in forum Revit Architecture - General
    Replies: 1
    Last Post: 2003-12-22, 07:43 AM

Posting Permissions

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