View Full Version : Float vs Decimal
BoKirra
2008-11-18, 11:30 AM
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.
lpseifert
2008-11-18, 01:07 PM
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
'gile'
2008-11-18, 09:12 PM
Hi,
(/ 10 3) returns 3
(float (/ 10 3)) returns 3.0
(/ 10 (float 3)) returns 3.3333
(/ 10 3.0) returns 3.3333
BoKirra
2008-11-18, 10:43 PM
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.
RobertB
2008-11-19, 01:58 AM
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.
irneb
2008-11-19, 05:51 AM
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).
BoKirra
2008-11-23, 10:30 PM
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.
BoKirra
2008-11-25, 01:28 AM
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.:Oops:
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.
;---------------------------------------------------------------------------------;
(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)
)
irneb
2008-11-25, 05:42 AM
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.
;---------------------------------------------------------------------------------;
(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.
irneb
2008-11-25, 05:47 AM
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.
BoKirra
2008-11-25, 06:21 AM
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.
;---------------------------------------------------------------------------------;
(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.
So based on your commons, my understanding was almost right.
But it is not necessary to use "float" function in this code because "getdist" always returns a float.
BTW, what if the variable takes datum (value) from an external data file?
Would that variable return a real or an integer?
In this case should I apply "flaot" to that variable?
Thanks with :beer:
irneb
2008-11-25, 06:52 AM
So based on your commons, my understanding was almost right.
But it is not necessary to use "float" function in this code because "getdist" always returns a float.That's correct!
BTW, what if the variable takes datum (value) from an external data file?
Would that variable return a real or an integer?
In this case should I apply "flaot" to that variable?Depends on how it's saved in the file & how you read it into a variable. Maybe in this case I'd use float just to be on the safe side.
Thanks with :beer:Slightly early for me, but thanks, have :beer::beer:
BoKirra
2008-11-26, 03:01 AM
...
Depends on how it's saved in the file & how you read it into a variable. Maybe in this case I'd use float just to be on the safe side.
Just another one:
What if the variable takes a value which entered by user in the dialog box?
Is it a real or integer?
Thanks, :beer::beer::beer:
irneb
2008-11-26, 06:01 AM
Just another one:
What if the variable takes a value which entered by user in the dialog box?
Is it a real or integer?
Thanks, :beer::beer::beer:If you mean a DCL dialog (not OpenDCL with some fancy number crunching), then the actual value returned is a string (or text). In which case you have to convert it to a integer / float with one of the following:
atoi - will change a string into an integer. Only the 1st possible number is converted, e.g. (atoi "3rthhfdh") will return 3. If it can't convert the string to a number it returns 0, e.g. (atoi "sdfgd") gives 0, or (atoi "") gives 0.
atof - will change a string into an real. Only the 1st possible number is converted, e.g. (atof "3rthhfdh") will return 3.0. If it can't convert the string to a number it returns 0.0, e.g. (atof "sdfgd") gives 0.0, or (atof "") gives 0.0.
read - will change the string (or at least the 1st part, with space delimeter) to whatever it can. E.g. (read "5.6 dsgfsgsdg") gives the real 5.6, (read "3 dffdgd") gives the integer 3, (read "3sf") gives a symbol with the name 3SF (not a string so be careful - you can check this with vl-symbolp).And then if in doubt, you could use type to check exactly what type of value is stored in a variable. E.g.(setq a 4 b 3.0 c (read "3sf")) ;Setup example variables
(type a) ;Gives INT
(type b) ;Gives REAL
(type c) ;Gives SYMYou use type in an if / cond statement by comparing it with a quoted version of its result, e.g. above (= (type b) 'REAL) would return T, while (= (type c) 'INT) returns nil. So you could simply do the following if you don't know what the value is (assume val is the value you've got from somewhere):(while (= (type val) 'LIST) (setq val (car val))) ;If list just do the 1st item
(cond
((= (type val) 'INT) (setq val (float val))) ;If integer convert to real
((= (type val) 'REAL)) ;If real leave as is
((= (type val) 'STR) ;If string do some further checking
(setq test (read val)) ;Convert to possible data type
(cond
((= (type test) 'INT) (setq val (float test))) ;If integer convert to real
((= (type test) 'REAL) (setq val test)) ;If real leave as is
(t (setq val (atof val))) ;Else return whatever atof would
) ;_ end of cond
)
(t (setq val 0.0)) ;Otherwise return zero as real
) ;_ end of cond
BoKirra
2008-11-27, 12:34 AM
If you mean a DCL dialog (not OpenDCL with some fancy number crunching), then the actual value returned is a string (or text). In which case you have to convert it to a integer / float with one of the following:
...
Thanks for your explanation.
BTW: I've heard that OpenDCL, but I haven't got into it. I'll learn it later.
Thanks again & have :beer::beer::beer:
Cheers!
irneb
2008-11-27, 07:56 AM
You're welcome :beer::beer:
OpenDCL IMHO is much better than normal DCL (in most cases). It's much simpler to actually create the dialog as it uses a graphic interface to do so (much like dialogs you create in VBA). But its real benefit is that the dialog can have much more complex tiles. Even some which load stuff like layers / dimstyles / etc on their own without you having to program anything to do this. It's probably going to be a bit confusing at first on how to use the dialog in LISP, but once you understand that it basically calls a routine each time the user clicks / changes something - this method becomes easier to use than the DCL version.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.