0

I am using the SQLite .NET library to save doubles

This is my pseudo-code

// create table t1 (value REAL)
// ... connect to db
var sql = $"insert into t1(value) VALUES ({double.MaxValue:G64})"; // insert into t1(value) VALUES (1.7976931348623157E+308)
using (var command = new SQLiteCommand(sql, con))
{
  command.ExecuteNonQuery();
}

Then I do a select:

// SELECT * FROM t1
// .. connect + query
var d = reader.GetDouble(0);

But the value of d is double.PositiveInfinity rather than double.MaxValue, what could be the reason for the difference?

Simon Goodman
  • 1,174
  • 1
  • 8
  • 35
  • And if you use parameter instead of converting value to string in insert? – Evk May 01 '18 at 12:24
  • 1
    It's better to use parameters anyway as the current code will break on any system not using the period symbol as decimal separator. – ckuri May 01 '18 at 12:44
  • @Evk, I get a `System.OverflowException`, I think the issue is that `double.MaxValue` in c# is not the same as `std::numeric_limits::max()` in c++ – Simon Goodman May 01 '18 at 16:53
  • @ckuri, yes, of course, but `double.MaxValue` does not use period/comma, the issue is more about the data not been saved properly on any system. – Simon Goodman May 01 '18 at 16:54
  • @SimonGoodman It does. If I run your code on my German machine I get "1,7976931348623157E+308" - notice the comma instead of the period. – ckuri May 01 '18 at 16:57
  • @ckuri, I see what you are saying, (I was looking at it as a whole number rather than the scientific notation), but in any case, that's not really the issue here as it does not work as a whole/scientific number or not. – Simon Goodman May 01 '18 at 17:27
  • Probably rounding errors. – CL. May 01 '18 at 18:09
  • @CL., no, the data is saved as `-Inf` or `Inf`, I think it has to do with c++ max double vs c# max double. They are slightly different. – Simon Goodman May 01 '18 at 19:27

0 Answers0