I will be receiving some SQLite database query parameters in JSON format but of course JSON only has a generic Number data type. I have experimented just using SetDatabaseDouble() every time and it seems to work but is it working by luck?
Is a Double with no fractional part the same as an Integer?
I know I could adapt the JSON to include a member that denotes the type but I'd rather avoid doing that if SetDatabaseDouble() will work for integers too (Double and Integer are the only number types I need).
Use SetDatabaseDouble() for an integer?
- the.weavster
- Addict
- Posts: 1537
- Joined: Thu Jul 03, 2003 6:53 pm
- Location: England
- NicTheQuick
- Addict
- Posts: 1227
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Use SetDatabaseDouble() for an integer?
It's not always the best solution because integers are more precise in the integers alone. In the code you can see that two different integer values result in the same double value.
Code: Select all
Define i.i, d.d
i = $7fffffffffffffff
d = i
Debug i
Debug d
i = $7ffffffffffffe00
d = i
Debug i
Debug d
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
- the.weavster
- Addict
- Posts: 1537
- Joined: Thu Jul 03, 2003 6:53 pm
- Location: England
Re: Use SetDatabaseDouble() for an integer?
It depends how big an int you need, NicTheQuick's examples both massively overflow the specification. It's not really surprising that it breaks down. The int part of the spec allows 52 bits not including the sign. In fact stackoverflow tells me that it should work fine all the way up to 2^53 because the exponent is still small(!)... See https://stackoverflow.com/questions/184 ... n-a-double
If you can remain within the limits of the spec, and check for out of bounds input data in your program, you should be ok because the int can be represented internally properly.
If you can remain within the limits of the spec, and check for out of bounds input data in your program, you should be ok because the int can be represented internally properly.
Code: Select all
Define i.i, d.d
; 2^52
i = 4503599627370496
d = i
Debug i
Debug d
; 2^53
i = 9007199254740992
d = i
Debug i
Debug d
; 2^53 + 1 is the first point of failure.
i = 9007199254740993
d = i
Debug i
Debug d
- the.weavster
- Addict
- Posts: 1537
- Joined: Thu Jul 03, 2003 6:53 pm
- Location: England
Re: Use SetDatabaseDouble() for an integer?
Thanks @spikey
I think I can live with that2^53 + 1 is the first point of failure.