Home > Articles

  • Print
  • + Share This
This chapter is from the book

Numbers

The common data types used for numbers include the following:

  • Integers

  • Floats

Integers

key_topic_icon.jpg

An integer (also known as int) is a positive or negative whole number (a number with no decimal points or fractions). For example, the following are positive integers: 3, 4096, and 65535. The following are negative integers (–2, –64, and –98765). What about 0? It’s also an integer.

The INT function in most programming languages drops the decimal or fractional value of a number such as 3.6 and leaves only the whole value (in this case, 3). This can be useful to obtain a whole number value after randomizing a range of numbers. The following example is written in Perl:

my $randnum = int(rand(100));
# int(rand(100)) discards decimal portion of randomized number
print "Here's a random number between 0 and 100: $randnum\n";

Floats

key_topic_icon.jpg

A float (also known as floating-point number) is a number that contains up to seven digits and has at least one decimal place. For example, the following are floats:

  • 5.56

  • .0275687

  • 3.14159

  • .303

A float is a single-precision, floating-point, 32-bit value.

Floats can also be expressed using powers of ten or powers of two. This way, a fixed number of digits can be used to express a very wide range of numeric values. Here are some examples:

  • 93.1×105 = 9,310,000

  • 93.1×10-5 = 0.000931

  • 93.1×1016 = 931,000,000,000,000,000

  • 93.1×10-16 = 0.00000000000000931

  • 47.67×212 = 47,670,000,000,000

  • 47.67×2-12 = 0.00000000004767

Singe-precision and double-precision floating point numbers are actually approximations of the true value of a number because of the rounding that takes place when non-integer numbers are used. The floating-point calculator at https://www.exploringbinary.com/floating-point-converter/ demonstrates this fact.

  • + Share This
  • 🔖 Save To Your Account