Movatterモバイル変換


[0]ホーム

URL:


bignum0.3.2

Hierarchy of data types

Source:vignettes/type-hierarchy.Rmd
type-hierarchy.Rmd

This vignette follows terminology outlined by thevctrs package. For furtherinformation, seehelp("faq-compatibility-types", package = "vctrs").

There are three numeric types in base R:logical,integer anddouble. They form a naturalhierarchy from the simplest (logical) to the richest(double), with richer types able to accommodate simplertypes without losing information.

  • integer expands the set of integer values supported bylogical.
  • double expands the set of integer values supported byinteger, andalso supports non-integervalues.

The bignum package provides two additional numeric types:biginteger andbigfloat. These aretype-compatible with the existing numeric types because they extend theset of possible values. However, the hierarchy becomes more complexbecause lossy casts are now possible.

  • biginteger expands the set of integer values supportedbydouble. In fact, it supportsany integer value(becausebiginteger uses arbitrary precision). But itdoes not support non-integer values.
  • bigfloat expands the set of values supported bydouble (both in precision and range), butdoes notsupport the entire range of integers supported bybiginteger (becausebigfloat uses fixedprecision).

Summary of numeric type hierarchies in base R and thebignum package. Arrows indicate the direction of richer data types.Dashed lines indicate the potential for lossy casts.

Type conversion and lossy casts

As discussed above, casting values from one type to another can loseinformation.

We see an example in base R, when we cast a non-integer or largedouble to aninteger:

# non-integer doubleas.integer(1.5)#> [1] 1# large doubleas.integer(1e10)#> Warning: NAs introduced by coercion to integer range#> [1] NA

For illustrative purposes, we now consider how lossy casts can affectbignum conversions:

library(bignum)# double -> bigintegeras_biginteger(1.5)#> Warning: Loss of precision while converting from `x` <double> to <biginteger>.#> Locations: 1#> <biginteger[1]>#> [1] 1# biginteger -> doubleas.double(biginteger(10)^16L)#> Warning: Loss of precision while converting from `x` <biginteger> to <double>.#> Locations: 1#> [1] 1e+16# bigfloat -> doubleas.double(bigfloat(1)/3)#> Warning: Loss of precision while converting from `x` <bigfloat> to <double>.#> Locations: 1#> [1] 0.3333333# bigfloat -> bigintegeras_biginteger(bigfloat(1.5))#> Warning: Loss of precision while converting from `x` <bigfloat> to <biginteger>.#> Locations: 1#> <biginteger[1]>#> [1] 1# biginteger -> bigfloatas_bigfloat(biginteger(10)^51L+1L)#> Warning: Loss of precision while converting from `x` <biginteger> to <bigfloat>.#> Locations: 1#> <bigfloat[1]>#> [1] 1e+51

[8]ページ先頭

©2009-2025 Movatter.jp