Skip to main content

Mathematics

Number​

A simple number block. You can type in any numerical value, including whole numbers, decimals, and negative numbers.

You can also type the special values Infinity or -Infinity to represent values of infinite size. In Code Kit, Infinity means a number that is bigger than any other number you can work with.

info

The Infinity value can be tricky to work with. It is used to represent a number that is larger than any finite number, but it can lead to unexpected results in some calculations. For example, adding Infinity to any number results in Infinity, and dividing by Infinity results in 0. It's important to be cautious when using Infinity in your projects.

Arithmetic​

The arithmetic block performs basic operations on two numbers.

  • + (addition): Adds two numbers together.
  • - (subtraction): Subtracts the second number from the first.
  • × (multiplication): Multiplies two numbers together.
  • ÷ (division): Divides the first number by the second.
  • ^ (power): Raises the first number to the power of the second.

Trigonometry​

The trigonometry block performs trigonometric functions on a number.

  • sin (sine): The sine of an angle.
  • cos (cosine): The cosine of an angle.
  • tan (tangent): The tangent of an angle.
  • asin (inverse sine): The angle whose sine is the given number.
  • acos (inverse cosine): The angle whose cosine is the given number.
  • atan (inverse tangent): The angle whose tangent is the given number.

All angles are measured in degrees.

Constants​

The constant block provides some common mathematical constants. These are special numbers that are always the same.

  • π (pi): The ratio of a circle's circumference to its diameter.
  • e (Euler's number): The base of the natural logarithm.
  • φ (golden ratio): A special number that appears in nature and art.
  • sqrt(2) (square root of 2): The length of the diagonal of a square with side length 1.
  • sqrt(1/2) (square root of 1/2): The length of the diagonal of a square with side length 1/2.
  • ∞ (infinity): A number that is bigger than any other number you can work with.

Number properties​

The number properties block tells you about a number and its qualities.

  • even: Is the number exactly divisible by 2?
  • odd: Is the number not exactly divisible by 2?
  • prime: Is the number only divisible by 1 and itself?
  • whole: Is the number an integer (whole number)?
  • positive: Is the number greater than zero?
  • negative: Is the number less than zero?
  • divisible by: Is the number exactly divisible by another number?

You provide a number as input, and the block will tell you if that number satisfies the selected property.

This block outputs a boolean value, which is either true or false. You can use this value in conditional statements to make decisions based on the properties of a number.

Round​

The round block provides different ways to round a number.

  • round: Rounds a floating-point (decimal) number to the nearest whole number.
  • round up: Rounds a number up to the nearest whole number. This is also called "ceiling".
  • round down: Rounds a number down to the nearest whole number. This is also called "floor".

Functions​

The functions block performs common mathematical functions on a number.

  • square root: The square root of a number.
  • absolute: The absolute value of a number.
  • negate: The negation of a number.
  • ln: The natural logarithm of a number.
  • log10: The base-10 logarithm of a number.
  • e^: The exponential function.
  • 10^: The exponential function.

Minimum / maximum​

This block returns either the smaller or larger of two values.

Remainder​

The remainder block gives you what's left over (the remainder) after dividing the first number by the second. This is also called the "modulo" operation.

Constrain​

The constrain block limits a number to a certain range.

  • constrain: The input number to constrain.
  • low: The minimum value the input can be. If the input is less than this, it will be set to this value.
  • high: The maximum value the input can be. If the input is greater than this, it will be set to this value.

Map​

The map block converts a number from one range to another.

  • map: The input number to convert.
  • from: The minimum and maximum values of the input range.
  • to: The minimum and maximum values of the output range.

This is useful when you need to convert data from one scale to another, like mapping sensor values to a more convenient range.

Random integer​

The random integer block generates a whole number between two values.

Random fraction​

The random fraction block generates a floating-point (decimal) number between 0 and 1.

Random seed​

Seed the random number generator so later random values start from a known state.

Bitwise operations​

Apply bitwise logic such as AND, OR, XOR, and shifts to integer values.

Bits and bytes​

These helper blocks let you inspect and modify individual bits or extract bytes from a larger value.

Bit value​

Read the value of a bit at a chosen position.

Bit functions​

Set, clear, or toggle a bit in an integer.

Write bit​

Write a chosen bit value at a given position.

High / low byte​

Extract the high byte or low byte from a larger value.

Moving average​

A moving average is a way to smooth out a series of numbers by averaging them over time. This can be useful for filtering out noise or detecting trends in data.

New moving average filter​

This block creates a new moving average filter with a given window size. The window size is the number of values to average together.

Add value to moving average​

This block adds a new value to a moving average filter.

Get moving average​

This block gets the current moving average from a filter.