r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
173 stars 27 forks source link

Round trip log exp changes units/values #292

Closed bart1 closed 2 years ago

bart1 commented 2 years ago

I find when I log and then exponent certain units change from km to m so then my values are a factor 1000 off (second example below). Conversions with values in 'm' work fine (first example below). My guess this has to do with the factor 1000 in the units of the log transformed number not being converted back correctly. I guess either outcome of the result being 100000 [m] or 100 [km] could work but now the result is wrong. A current work around is to change the units to meters before hand.

require(units)
#> Loading required package: units
#> udunits database from /usr/share/xml/udunits/udunits2.xml
# first
(m<-set_units(100,'m'))
#> 100 [m]
log(m)
#> 4.60517 [ln(re 1 m)]
exp(log(m))
#> 100 [m]
# second
(m<-set_units(100,'km'))
#> 100 [km]
log(m)
#> 4.60517 [ln(re 1000 m)]
exp(log(m))
#> 100 [m]
# third
(m<-set_units(100,'km'))
#> 100 [km]
m<-`units<-`(m,'m')
log(m)
#> 11.51293 [ln(re 1 m)]
exp(log(m))
#> 1e+05 [m]
sessionInfo()
#> R version 4.1.2 (2021-11-01)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=nl_NL.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=nl_NL.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=nl_NL.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] units_0.7-2
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.7      knitr_1.34      magrittr_2.0.1  rlang_0.4.12   
#>  [5] fastmap_1.1.0   fansi_0.5.0     stringr_1.4.0   styler_1.6.1   
#>  [9] highr_0.9       tools_4.1.2     xfun_0.27       utf8_1.2.2     
#> [13] withr_2.4.2     htmltools_0.5.2 ellipsis_0.3.2  yaml_2.2.1     
#> [17] digest_0.6.28   tibble_3.1.5    lifecycle_1.0.1 crayon_1.4.1   
#> [21] purrr_0.3.4     vctrs_0.3.8     fs_1.5.0        glue_1.4.2     
#> [25] evaluate_0.14   rmarkdown_2.11  reprex_2.0.1    stringi_1.7.5  
#> [29] compiler_4.1.2  pillar_1.6.4    backports_1.2.1 pkgconfig_2.0.3

Created on 2021-11-10 by the reprex package (v2.0.1)

Enchufa2 commented 2 years ago

Thanks for the report!

bart1 commented 2 years ago

Thanks for fixing and the great work!