Lessons

Number Manipulation: Basic

Welcome to this lesson on basic number manipulation in AutoLISP. When dealing with numbers, it’s essential to have tools that allow us to perform straightforward operations, whether it’s incrementing a value, getting the absolute value of a number, or converting between integers and floating-point numbers. This lesson will introduce you to some of the primary functions in AutoLISP that aid in these tasks.


Learning Outcomes

By the end of this lesson, students will be able to:

  • Recognize and Apply Basic Number Manipulation Functions: Understand the significance and usage of the 1+, 1-, abs, fix, and float functions.
  • Perform Arithmetic Adjustments: Increment and decrement numbers with ease using the respective functions.
  • Understand Number Types: Differentiate between integers and floating-point numbers and convert between them.
  • Apply Real-World Scenarios: Use the provided functions in practical coding situations, enhancing the accuracy and efficiency of number-related tasks.

Number Manipulations: Basic refers to the fundamental operations and functions in programming that allow developers to modify, adjust, or derive new values from existing numbers. These manipulations encompass a range of tasks, from simple arithmetic adjustments like incrementing or decrementing a value to more nuanced operations like converting between different number types or retrieving the absolute value of a number. Mastering these basic number manipulations is essential for any programmer, as they form the cornerstone for more complex numerical computations and logic in code. In the context of AutoLISP, there are specific functions designed to facilitate these basic manipulations, ensuring precision and efficiency in numeric-related tasks.


1+ (increment)

The 1+ function is used to increment a number by one.

Syntax:

(+ [number number …])

Example 1:

(setq val 5)
(1+ val) ; This will return 6

In this code, we assign the value 5 to the variable val. Using the 1+ function, we increment this value by one, resulting in 6.

Example 2:

(1+ 9.75) ; This will return 10.75

Here, we directly increment the floating-point number 9.75 by one, giving us 10.75.

Example 3:

(+ 7 5)
12(+ 10 20 50 4.5)
84.5(+ 1 10 3 7.0)
21.0

 

1- (decrement)

The 1- function, as you might guess, decrements a number by one.

Syntax:

(- [number number …])

Example 1:

(setq val 10)
(1- val) ; This will return 9

We start with the value 10 for our variable val. By using the 1- function, we decrease this value by one, resulting in 9.

Example 2:

(1- 7.25) ; This will return 6.25

In this instance, we decrement the floating-point number 7.25 by one, yielding 6.25.

Example 3:

(- 70 30)
40(- 70 50.0)
20.0(- 70 60.0 4.8)
5.2(- 7)
-7

abs

The abs function returns the absolute value of a number, eliminating any negative sign.

Syntax:

(abs number)

Example 1:

(abs -5) ; This will return 5

We’ve taken the absolute value of -5, which results in 5.

Example 2:

(abs 3.14) ; This will return 3.14

For positive numbers, the abs function will return the number unchanged. So, 3.14 remains 3.14.

Example 3:

(abs 70)
70(abs -70)
70

(abs -77.35)
77.35


fix

The fix function converts a real number to its nearest integer. If the real number is positive, fix will round down. If it’s negative, fix will round up.

Syntax:

(fix number)

Example 1:

(fix 7.75) ; This will return 7

Here, the number 7.75 is rounded down to 7.

Example 2:

(fix -3.25) ; This will return -3

In this example, the negative number -3.25 is rounded up to -3.


float

The float function converts an integer to a floating-point number.

Syntax:

(float number)

Example 1:

(float 8) ; This will return 8.0

Here, the integer 8 is converted to the floating-point number 8.0.

Example 2:

(float -4) ; This will return -4.0

Similarly, the integer -4 is converted to the floating-point number -4.0.


In conclusion, understanding these basic number manipulation functions in AutoLISP is crucial for any programmer. They allow for swift adjustments to numerical values and facilitate the transition between different types of numbers. Remember to continually practice using these functions and apply them in various coding scenarios. As you continue your journey in AutoLISP, these tools will prove indispensable. Happy coding!