Lesson 3
Clojure Numbers
Welcome to Clojure Numbers

Welcome back! You've previously learned about the basics of Clojure and how to work with simple data types. Now, let's move forward and explore another important aspect of programming with Clojure: numbers. Understanding different types of numbers and how to manipulate them is key to managing various elements in our Clojure shooter game.

What You'll Learn

In this lesson, we will dive into the various types of numbers available in Clojure and how they interact with each other. Specifically, we will cover:

  • Different types of numbers such as Integers, Big Integers, Ratios, Big Decimals, and Floating Points.
  • How to mix and operate with different numeric types.
  • Handling arithmetic overflow and utilizing Clojure's auto-promotion capabilities to manage large numbers.

By the end of this lesson, you'll be comfortable handling numeric operations in Clojure, a skill that will allow you to manage game statistics, perform complex calculations, and much more.

Here's a quick look at some code you’ll be able to understand and write:

Clojure
1;; Different types of numbers 2(def player-health 100) ;; Integer (Contagiousness 0 - Lowest) 3(def max-health 100N) ;; Big Integer (Contagiousness 1) 4(def damage-ratio 10/2) ;; Ratio (Contagiousness 2) 5(def weapon-accuracy 0.8M) ;; Big Decimal (Contagiousness 3) 6(def enemy-distance 20.5) ;; Floating Point (Contagiousness 4 - Highest) 7 8;; Mixing numbers of different types 9(def mixed-sum-1 (+ player-health max-health)) ;; Big Integer 10(def mixed-sum-2 (+ player-health max-health damage-ratio)) ;; Ratio 11(def mixed-sum-3 (+ player-health max-health damage-ratio weapon-accuracy)) ;; Big Decimal 12(def mixed-sum-4 (+ player-health max-health damage-ratio weapon-accuracy enemy-distance)) ;; Floating Point 13 14;; Overflow and auto-promoting to big integer 15;; (def score-overflow (* 4037000499 3037000500)) ;; ArithmeticException integer overflow 16 17(def auto-promoted-score (*' 4037000499 3037000500)) ;; Big Integer
Numeric Types in Clojure

Below is a table summarizing different numeric types in Clojure:

TypeRange and ImplementationSyntax ExamplesContagiousness
Integer32-bit or 64-bit (platform dependent)42, -7, 00 (Lowest)
Big IntegerArbitrarily large and precise integers42N, 1000N1
RatioInfinite fractional numbers, stored as two big integers3/4, 10/22
Big DecimalArbitrarily precise floating-point numbers0.5M, 123.45M3
Floating Point64-bit IEEE 754 double-precision floating-point numbers3.14, 1e-34 (Highest)

Contagiousness in this context refers to how operations involving mixed numeric types in Clojure will result in a type promotion to the "higher" type. For example, if you add an Integer and a Big Decimal, the result will be a Big Decimal because Big Decimal is more "contagious" (higher up in the hierarchy) than an Integer. The numerical types with higher contagiousness will impose their type on the result.

Why It Matters

Numbers are fundamental to almost all programming tasks. In our game, keeping track of the hero's health, calculating damage, measuring distances, and maintaining accuracy all rely on correctly managing numbers. Whether you're adding score points, calculating damage, or working with large data values, understanding how to handle different types of numbers and avoid pitfalls like arithmetic overflow is crucial.

Mastering these concepts ensures that your program runs efficiently and accurately, enhancing both your game development and overall programming skills. Let's dive into the practice section and explore these numeric types and operations in depth!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.