ArticleslgStudy

computer science

Decimal data type

Decimal data type is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Decimal data type rather than just read about it. In short: Some programming languages (or compilers for them) provide a built-in (primitive) or library decimal data type to represent non-repeating decimal fractions like 0.3 and −1.17 without rounding, and to do arithmetic on them. Examples are the decimal.Decimal or num7.Num type of Python, and analogous types provided by other languages.

Key takeaways

  • Decimal data type belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Decimal data type to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Decimal data type from memory before moving on to harder problems.

Reference excerpt

Some programming languages (or compilers for them) provide a built-in (primitive) or library decimal data type to represent non-repeating decimal fractions like 0.3 and −1.17 without rounding, and to do arithmetic on them. Examples are the decimal.Decimal or num7.Num type of Python, and analogous types provided by other languages.

Rationale Fractional numbers are supported on most programming languages as floating-point numbers or fixed-point numbers. However, such representations typically restrict the denominator to a power of two. Most decimal fractions (or most fractions in general) cannot be represented exactly as a fraction with a denominator that is a power of two. For example, the simple decimal fraction 0.3 (⁠3/10⁠) might be represented as ⁠5404319552844595/18014398509481984⁠ (0.299999999999999988897769…). This inexactness causes many problems that are familiar to experienced programmers. For example, the expression 0.1 * 7 == 0.7 might counterintuitively evaluate to false in some systems, due to the inexactness of the representation of decimals. Although all decimal fractions are fractions, and thus it is possible to use a rational data type to represent it exactly, it may be more convenient in many situations to consider only non-repeating decimal fractions (fractions whose denominator is a power of ten). For example, fractional units of currency worldwide are mostly based on a denominator that is a power of ten. Also, most fractional measurements in science are reported as decimal fractions, as opposed to fractions with any other system of denominators. A decimal data type could be implemented as either a floating-point number or as a fixed-point number. In the fixed-point case, the denominator would be set to a fixed power of ten. In the floating-point case, a variable exponent would represent the power of ten to which the mantissa of the number is multiplied. Languages that support a rational data type usually allow the construction of such a value from two integers, instead of a base-2 floating-point number, due to the loss of exactness the latter would cause. Usually the basic arithmetic operations (+, −, ×, ÷, integer powers) and comparisons (=, <, >, ≤, ≥, ≠) would be extended to act on them—either natively or through operator overloading facilities provided by the language. These operations may be translated by the compiler into a sequence of integer machine instructions, or into library calls. Support may also extend to other operations, such as formatting, rounding to an integer or floating point value, etc.

Standard formats IEEE 754 specifies three standard floating-point decimal data types of different precision, introduced in the 2008 revision of the standard (also adopted as ISO/IEC/IEEE 60559:2011):

Decimal32 floating-point format Decimal64 floating-point format Decimal128 floating-point format

Language support C# has a built-in data type decimal consisting of 128 bits resulting in 28–29 significant digits. It has an approximate range of ±1.0 × 10−28 to ±7.9228 × 1028. Starting with Python 2.4, Python's standard library includes a Decimal class in the module decimal. Ruby's standard library includes a BigDecimal class in the module bigdecimal. Java's standard library includes a java.math.BigDecimal class. In Objective-C, the Cocoa and GNUstep APIs provide an NSDecimalNumber class and an NSDecimal C data type for representing decimals whose mantissa is up to 38 digits long, and exponent is from −128 to 127. Some IBM systems and SQL systems support DECFLOAT format with at least the two larger formats. ABAP's new DECFLOAT data type includes decimal64 (as DECFLOAT16) and decimal128 (as DECFLOAT34) formats. PL/I natively supports both fixed-point and floating-point decimal data. GNU Compiler Collection (gcc) provides support for decimal floats as an extension to C and C++.

See also Arbitrary-precision arithmetic Floating-point arithmetic Floating-point error mitigation

References

Worked examples

Example 1 — a first encounter with Decimal data type

Start with the simplest possible case. Write down what Decimal data type claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Decimal data type before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Decimal data type ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Decimal data type

In research
Decimal data type appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Decimal data type in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Decimal data type is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer arithmetic, Data types, so understanding it makes those chapters shorter.
In everyday life
Look for Decimal data type outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Decimal data type” →

How to study Decimal data type in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Decimal data type means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Decimal data type out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Decimal data type in simple terms?

Some programming languages (or compilers for them) provide a built-in (primitive) or library decimal data type to represent non-repeating decimal fractions like 0.3 and −1.17 without rounding, and to do arithmetic on them. Examples are the decimal.Decimal or num7.Num type of Python, and analogous t…

Why does Decimal data type matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Decimal data type?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Decimal data type.

Tags

  • Computer arithmetic
  • Data types

Keep exploring