Bug 15234

Summary: Hexadecimal literals are not parsed correctly
Product: R Reporter: Bogumil Kaminski <bogumil.kaminski>
Component: LanguageAssignee: R-core <R-core>
Status: CLOSED FIXED    
Severity: normal CC: murdoch
Priority: P5    
Version: R 2.15.0   
Hardware: x86_64/x64/amd64 (64-bit)   
OS: Windows 64-bit   

Description Bogumil Kaminski 2013-03-12 08:47:54 UTC
GNU R does not parse floating point hexadecimal literals correctly.

In notation without exponent part GNU R discards decimal points when parsing hexadecimal literals:

0x111
# Result: 273
# Status: OK

0x11.1
# Result: 273
# Status: should be 17.0625

0x1.1.1
# Result: 273
# Status: should raise an error

0x....1....1....1
# Result: 273
# Status: should raise an error

In notation with exponent ([pP] option) last decimal point is taken into consideration and all earlier decimal points are ignored:

0x11.1p0
# Result: 17.0625
# Status: OK

0x1.1.1p0
# Result: 17.0625
# Status: should raise an error

0x....1....1....1p0
# Result: 17.0625
# Status: should raise an error

Additionally hexadecimal literals are documented very briefly in "R Language Definition" document.

Bug manifests itself on Windows:

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Polish_Poland.1250  LC_CTYPE=Polish_Poland.1250   
[3] LC_MONETARY=Polish_Poland.1250 LC_NUMERIC=C                  
[5] LC_TIME=Polish_Poland.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

and under Linux:

> sessionInfo() 
R version 2.15.1 (2012-06-22) 
Platform: x86_64-pc-linux-gnu (64-bit) 
 
locale: 
[1] C 
 
attached base packages: 
[1] stats     graphics  grDevices utils     datasets  methods   base      
 
other attached packages: 
[1] seqinr_3.0-6 ade4_1.5-0
Comment 1 Duncan Murdoch 2013-05-12 16:40:28 UTC
Our documentation doesn't say we support hex literals for floating point values, so all of these could be treated as syntax errors.  However, we do support C99 notation "0x11.1p0" as you saw, so really the bugs here are that we aren't catching malformed literals like 0x11.1, or 0x1.1.1, etc., and that we don't document that we expect C99 conventions.
Comment 2 Duncan Murdoch 2013-05-12 18:40:16 UTC
Fixed in R-devel as of r62736.  I don't plan to backport to R-patched, since well-formed constants were being recognized properly, this only makes the parsing more strict.