Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Decimal data type

From Wikipedia, the free encyclopedia
Data type for storing floating-point numbers in base-10

Someprogramming languages (or compilers for them) provide a built-in (primitive) or librarydecimaldata type to represent non-repeatingdecimal fractions like 0.3 and −1.17 without rounding, and to do arithmetic on them. Examples are thedecimal.Decimal ornum7.Num type ofPython, and analogous types provided by other languages.

Rationale

[edit]

Fractional numbers are supported on most programming languages asfloating-point numbers orfixed-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 as5404319552844595/18014398509481984 (0.299999999999999988897769…). This inexactness causes many problems that are familiar to experienced programmers. For example, the expression0.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 arefractions, and thus it is possible to use arational 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 afloating-point number or as afixed-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 (+,,×,÷, integerpowers) and comparisons (=,<,>,,,) would be extended to act on them—either natively or throughoperator overloading facilities provided by the language. These operations may be translated by thecompiler into a sequence of integermachine instructions, or intolibrary calls. Support may also extend to other operations, such as formatting, rounding to an integer orfloating point value, etc.

Standard formats

[edit]

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

Language support

[edit]
  • C# has a built-in data typedecimal consisting of 128 bits resulting in 28–29 significant digits. It has an approximate range of ±1.0 × 10−28 to ±7.9228 × 1028.[1]
  • Starting with Python 2.4,Python's standard library includes aDecimal class in the moduledecimal.[2]
  • Ruby's standard library includes aBigDecimal class in the modulebigdecimal.
  • Java's standard library includes ajava.math.BigDecimal class.
  • InObjective-C, theCocoa andGNUstepAPIs provide anNSDecimalNumber class and anNSDecimal 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.[3]
  • ABAP's new DECFLOAT data type includes decimal64 (as DECFLOAT16) and decimal128 (as DECFLOAT34) formats.[4]
  • 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++.[5]

See also

[edit]

References

[edit]
  1. ^"Floating-point numeric types - C# reference". 29 September 2022.
  2. ^"Decimal — Decimal fixed point and floating point arithmetic — Python 3.10.0 documentation".
  3. ^"Data management".IBM.
  4. ^"How the new ABAP Data Type DECFLOAT helps computing complex calculation? « SAP Ignite". Archived fromthe original on 2012-08-09. Retrieved2012-07-28.
  5. ^"GCC Manual". 6.13 Decimal Floating Types.
Uninterpreted
Numeric
Reference
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Decimal_data_type&oldid=1336546449"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp