liveBook · Manning

Type promotion

As explained in Clojure: The Essential Reference

Type promotion is the conversion of operands or results to a numeric type capable of representing a required level of precision or range.

Type promotion converts operands or results to a numeric type able to represent the required precision or range. In Clojure arithmetic, the result generally follows the most “contagious” operand type: a more precise numeric operand influences the operation’s result. section 5.13

The practical rule is:

  • Mixed numeric types affect the type of an arithmetic result. section 5.13
  • Greater precision generally propagates through the operation. section 5.13
  • Promotion is distinct from overflow behavior: unchecked integer operations can wrap at the limits of their resulting type. section 5.13

Converting a rational result

An explicit conversion makes the resulting representation visible: `(double (- 10 1/3))` produces `9.666666666666668`, rather than retaining the exact rational form.

How Clojure: The Essential Reference covers it

How deep it goes
Defines the contagion rule and illustrates conversion, but does not enumerate the complete precedence table.
Examples
Uses unchecked arithmetic, signed-long bounds, and conversion of a rational result to double.
What it assumes
Assumes familiarity with numeric types and precision while relating them to arithmetic behavior.
What you can do after
Evaluate how numeric types influence arithmetic results and representation.
How it connects
Connects numeric operators with precision, integer overflow, and hashing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.