Skip to content

dbQuoteLiteral() for numerics has insufficient precision in some cases #404

Open
@krlmlr

Description

@krlmlr
library(DBI)

data <- 0.0027392760273972603
sql <- dbQuoteLiteral(ANSI(), data)

sql
#> <SQL> 0.00273927602739726
as.numeric(sql) - data
#> [1] -4.336809e-19

sql_exp <- sprintf("%.17e", data)

sql_exp
#> [1] "2.73927602739726037e-03"
as.numeric(sql_exp) - data
#> [1] 0

Created on 2022-12-09 with reprex v2.0.2

We could use sprintf("%.17e") as in the reprex to always convert to exponential notation with enough decimal numbers.

@hannes: Are you aware of databases that would complain about numbers passed in exponential notation in situations when they wouldn't complain about decimal numbers?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions