1. Login to Give a bone

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

2. Login to Give a bone

## 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. Login to Give a bone

## 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. Login to Give a bone

## Re: Float vs Decimal

Originally Posted by lpseifert
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.

5. Login to Give a bone

## Re: Float vs Decimal

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

My question are:
1) what is the difference in LISP application.
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.

6. Login to Give a bone

## Re: Float vs Decimal

• 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. Login to Give a bone

## Re: Float vs Decimal

Originally Posted by irneb
• 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. Login to Give a bone

## Re: Float vs Decimal

Originally Posted by irneb
• 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. Login to Give a bone

## 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. Login to Give a bone

## 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 Last

#### Posting Permissions

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