PDA

View Full Version : Autocad Math?


Anthony Rhodes
2008-11-26, 08:08 PM
I just started working my way through Jos van Doorn's Autolisp Course (See Attached) I am working the exercise for lesson 3 ( pg 11) I made my way through the exercies and everything worked perfect. Since I do not have the actual answers for the exercise I used my calculater to check the answer. I came up with 11.76 and autocad is comming up with 11. What am I doing wrong?


Here is the exercise copied from the book:


In lesson 1 we did a calculation. Now let's do another calculation. I want to divide the number 9632 by the product of 63 times 13.

But lets do it in a proper way. I want to make the calculation in one line. And I want to store values in the CC and PR variable.


Below was the code I typed to get the answer:


(setq cc(/ 9632 (setq pr(* 63 13))))


Does anyone see what I have done wrong? Why does autocad report the answer as 11 and my calcualtor says its 11.76

PS: Permission to share the book is given on pg 3 inside of the book

Thanks for your help,

T.Willey
2008-11-26, 08:20 PM
With Lisp if you do any math function with integers only, then the answer will be in integers. If you want the answer to be a real, the just add a decimal to one of the numbers, which will represent that number as a real.

Example
(/ 5 2) = 2
(/ 5 2.) = 2.5

T.Willey
2008-11-26, 08:27 PM
The real answer might even have more decimal places than it shows, which is most likely. What are you trying to do? Just learn? If so, then know that the variable will hold the whole number, but what it prints to the command line will only show a certain amount of decimal places. I think it is one of the ' dim ' variables. DimZim or something like that, but that might only respond to when you convert the real number to a string.

Anthony Rhodes
2008-11-26, 08:28 PM
I think we are both trying to type and answer at the same time. Right now I am just trying to learn so I will keep it simple for now. I had expected to see the number on the command line follow the units setting in autocad. That is where I went wrong. I am sure I will pick up more as I get past lesson #3 and ask more questions around here.

Thanks for your time,

devitg.89838
2008-11-30, 07:25 PM
Hi , could you give me the link to such LISP course.?

Anthony Rhodes
2008-12-01, 06:15 PM
Hi , could you give me the link to such LISP course.?

I thought I had uploaded it in the first post but I was wrong. I will try again.

irneb
2008-12-01, 07:15 PM
Most (if not all) programming languages use specific data types to store information in RAM. With numbers you get 2 basic data types, integers and reals. This was originally done to save the amount of RAM used. If you have fractions then reals are the only one to show the number with any kind of accuracy, the various types of real just gives more / less accuracy. Integers disregard anything beyond the decimal point.

Now, most programming languages compares the data types before perforimg an action on them - so it gives back a result the same as the input type. So if you sent the division function (/ ...) a set of integers, it's going to return an integer. You do get programming languages working more like a calculator, BTW a calc always uses a REAL to store any number (even something like 2 is actually stored in the calc's RAM as 2.0, or rather 2e+0). "Un"-fortunately LISP doesn't use only reals, it's an old programming language from the '70s - when PC's (or whatever they had then) had minuscule RAM - so allowing the programmer to use Integers when possible so less RAM could be used to do the same thing.