吃什么养肝护肝效果最好| 睡觉磨牙是什么原因引起的| 唏嘘什么意思| 红蜘蛛用什么药最有效| 胃气胃胀吃什么药最好| 小觑是什么意思| 醋酸泼尼松片治什么病| 苯甲酸钠是什么东西| 剖腹产后可以吃什么水果| 菩提根是什么| 居里夫人发明了什么| 咕咾肉是什么肉| vane是什么意思| skp什么意思| 林俊杰的粉丝叫什么| 葡萄打什么药| 经常落枕是什么原因引起的| 肺部有空洞是什么病症| 血气分析是检查什么的| 胃癌挂什么科| 生发吃什么食物好| 监制是干什么的| 可见原始心管搏动是什么意思| exp是什么日期| 十加一笔是什么字| ad吃到什么时候| rh血型阳性是什么意思| 照影是什么意思| 发冷是什么原因| 草果是什么| 预产期是什么意思| 舌头无苔是什么原因| 脂膜炎是什么原因引起的| 日语为什么| 为什么有眼袋是什么原因引起的| 不宁腿是什么症状| 夏天吃什么水果最好| 鸡毛菜是什么菜| 病毒性肠胃炎吃什么药| 干咳吃什么药最有效| 气血不足吃什么调理| 核桃什么时候吃最好| 成功的反义词是什么| 四大才子是什么生肖| vivo是什么品牌手机| 润喉咙什么东西最合适| 男士脸黑用什么能美白| 眩晕去医院挂什么科室| lsp什么意思| 甲减是什么原因引起的| 际会是什么意思| 黑匣子是什么颜色| 谷朊粉是什么| 比五行属什么| 投喂是什么意思| 榴莲和什么相克| dem是什么| 妇科炎症用什么药好| 经常抽筋是什么原因| 心经讲的是什么| 洁身自爱是什么生肖| 小孩牙疼吃什么药| 肚脐眼红是什么原因| 彼此彼此什么意思| 脾脏是人体的什么器官| 恭敬地看的词语是什么| 什么叫低级别上皮内瘤变| 肾阳不足吃什么中成药| 什么是碱性磷酸酶高怎么回事| 维生素b2有什么作用和功效| 交期是什么意思| 欲盖弥彰是什么意思| 猪精是什么意思| 什么是菜花病| 无产阶级是什么意思| 麂皮是什么材质| 40min是什么意思| 胃炎吃什么中药效果好| 脖子痛挂什么科| 12月15号是什么星座| 四维彩超什么时候做| 男人射精快什么原因| 大腿青筋明显是什么原因| 吃什么水果能长高| 精神病的前兆是什么| 什么样的镜子| 阴囊是什么| 三维彩超主要检查什么| 乳酪和奶酪有什么区别| 3月20日什么星座| 女性绝经有什么征兆| 胃反流吃什么药好| 梧桐树的叶子像什么| 每天坚持黄瓜敷脸有什么效果| 卫校有什么专业| au750是什么意思| 撒野是什么意思| 什么样的手相最有福气| 胃酸吃什么| 厘清和理清的区别是什么| 困是什么原因| 上眼皮肿是什么原因| 血小板低是什么原因造成的| 酸西地那非片是什么药| 湖南湖北以什么湖为界| 什么补蛋白最快的食物| 秦始皇原名叫什么名字| 碳13和碳14有什么区别| 龟头炎用什么药膏好| 阴茎进入阴道是什么感觉| 香蕉和什么不能一起吃| 喘息是什么意思| 吃什么可以提高血小板| 吃中药忌口都忌什么| tv是什么意思| 学前班是什么意思| 香槟酒属于什么酒| 心字底的字与什么有关| 脸颊红是什么原因| 什么树叶| 月泉读什么| 胃疼胃胀吃什么药| 感冒引起的喉咙痛吃什么药| 苏木是什么意思| 后背疼应该挂什么科| 纳米丝是什么面料| 乳房胀痛什么原因| 12月生日是什么星座| 拔罐的原理是什么| 中暑吃什么药见效快| 56年属什么| 肚子怕冷是什么原因该怎么办| 胰腺炎是什么症状| 孕检挂什么科| 深入交流是什么意思| 瓜娃子是什么意思| 瞳孔扩散意味着什么| 郁是什么意思| 凉皮加什么才柔软筋道| 什么动作容易怀孕| 腮腺炎用什么药| 宝宝老是摇头是什么原因| 手指头肿胀是什么原因| 胶囊是什么原料做的| 牙龈溃烂是什么原因| art是什么意思| 什么药治鼻炎| 冥想什么意思| bottle是什么意思| 1月3号什么星座| 阴煞是什么意思| 卷腹是什么| 做梦梦见鱼是什么意思| 上发条是什么意思| 莳花弄草是什么意思| 乳糖不耐受是什么原因导致的| 泼皮是什么意思| tg是什么指标| 狡兔三窟是什么生肖| 闲是什么生肖| 我要控制我自己是什么歌| 今天是什么纪念日| 1222是什么星座| 心脏搭桥和支架有什么区别| 下巴肿大是什么原因| 流口水是什么病的前兆| 一月十八号是什么星座| 纳米丝是什么面料| 加德纳菌阳性是什么意思| 依托考昔片是什么药| 报告是什么意思| 缺钾吃什么食物补得最快| 为什么得疱疹病毒| 胃痛胃胀什么原因引起的| 为什么会连续两天遗精| 腺样体挂什么科| 火车票无座是什么意思| 冬至说什么祝福语| 什么茶养肝护肝| 错构瘤是什么| 什么叫主动脉硬化| 血小板分布宽度偏低是什么意思| xpe是什么材料| 口周读什么| alpaca是什么意思| 松果体囊肿是什么病| 新白娘子传奇许仙为什么用女的演| 云南为什么叫云南| 海米是什么| 秉字五行属什么| 淋巴细胞浸润是什么意思| 心脏是由什么组织构成的| 白血球低吃什么补得快| 财库是什么意思| 肾上腺挂什么科| 什么时间最容易怀孕| 女人脑供血不足吃什么| 冥想是什么| 尿液里白细胞高是什么原因| 人造革是什么材质| 毛孔粗大是什么原因引起的| 招财猫鱼吃什么| zara是什么品牌| 淋巴结核是什么病| 山本耀司的品牌叫什么| 吐完后胃不舒服应该吃什么呢| 肠胃炎吃什么食物好| 啐了一口是什么意思| 门庭若什么| 烂尾楼是什么意思| 什么可以解酒最快方法| 成人发烧吃什么退烧药| 妊娠期是指什么时候| 掉头发去医院挂什么科| 高玩是什么意思| 9.7号是什么星座| soho是什么意思| 阳历7月份是什么星座| 轻奢什么意思| 老出虚汗是什么原因| dragon是什么意思| 光阴是什么意思| 蛀牙挂什么科| 小孩的指甲脱落是什么原因| ccr是什么意思| 碗打碎了预示着什么| 什么梗| fwb是什么意思| 6是什么意思| 景字属于五行属什么| 五指毛桃有什么用| 保税区是什么意思| 谍影重重4为什么换主角| 高血糖什么原因引起| 18罗汉都叫什么名字| 一什么明月| naco是什么牌子| 40周年是什么婚| 什么是黄油| 腿疼挂什么科| 两榜进士是什么意思| 刮痧是什么| 额是什么意思| 今日什么冲什么生肖| 小产后可以吃什么水果| 蛇蛋是什么样子的| 疱疹性咽峡炎吃什么药最管用| q热是什么病| 最近爆发什么病毒感染| 被毒蛇咬了有什么症状| 夏令时什么意思| 华国锋为什么辞职| 什么动物没有眼睛| 12.31什么星座| 百香果吃了有什么好处| 大明湖畔的夏雨荷是什么意思| 百田森的鞋什么档次| 栀子黄是什么| 奇脉见于什么病| 孩子爱咬指甲是什么原因| 梦见两条大蟒蛇是什么征兆| 老年斑用什么药膏可以去掉| 南瓜与什么食物相克| 祥林嫂是什么样的人| 百度
Small. Fast. Reliable.
Choose any three.
Floating Point Numbers
Table Of Contents

文化部关于举办第八届中国少年儿童合唱节的通知

百度 在凯达格兰大道的抗议结束后,台大兽医系教授周崇熙代表台大师生向台当局递交“陈情书”,并将台大的标志“傅钟”看板送给,展现捍卫与坚守大学自主的决心。

SQLite stores integer values in the 64-bit twos-complement format¹. This gives a storage range of -9223372036854775808 to +9223372036854775807, inclusive. Integers within this range are exact.

So-called "REAL" or floating point values are stored in the IEEE 754 Binary-64 format¹. This gives a range of positive values between approximately 1.7976931348623157e+308 and 4.9406564584124654e-324 with an equivalent range of negative values. A binary64 can also be 0.0 (and -0.0), positive and negative infinity and "NaN" or "Not-a-Number". Floating point values are approximate.

Pay close attention to the last sentence in the previous paragraph:

Floating point values are approximate.

If you need an exact answer, you should not use binary64 floating-point values, in SQLite or in any other product. This is not an SQLite limitation. It is a mathematical limitation inherent in the design of floating-point numbers.


¹ Exception: The R-Tree extension stores information as 32-bit floating point or integer values.

1.1. Floating-Point Accuracy

SQLite promises to preserve the 15 most significant digits of a floating point value. However, it makes no guarantees about the accuracy of computations on floating point values, as no such guarantees are possible. Performing math on floating-point values introduces error. For example, consider what happens if you attempt to subtract two floating-point numbers of similar magnitude:

1152693165.1106291898
-1152693165.1106280772

0.0000011126

The result shown above (0.0000011126) is the correct answer. But if you do this computation using binary64 floating-point, the answer you get is 0.00000095367431640625 - an error of about 14%. If you do many similar computations as part of your program, the errors add up so that your final result might be completely meaningless.

The error arises because only about the first 15 significant digits of each number are stored accurately, and the first difference between the two numbers being subtracted is in the 16th digit.

1.2. Floating Point Numbers

The binary64 floating-point format uses 64 bits per number. Hence there are 1.845e+19 different possible floating point values. On the other hand there are infinitely many real numbers in the range of 1.7977e+308 and 4.9407e-324. It follows then that binary64 cannot possibly represent all possible real numbers within that range. Approximations are required.

An IEEE 754 floating-point value is an integer multiplied by a power of two:

M × 2E

The M value is the "mantissa" and E is the "exponent". Both M and E are integers.

For Binary64, M is a 53-bit integer and E is an 11-bit integer that is offset so that represents a range of values between -1074 and +972, inclusive.

(NB: The usual description of IEEE 754 is more complex, and it is important to understand the added complexity if you really want to appreciate the details, merits, and limitations of IEEE 754. However, the integer description shown here, while not exactly right, is easier to understand and is sufficient for the purposes of this article.)

1.2.1. Unrepresentable numbers

Not every decimal number with fewer than 16 significant digits can be represented exactly as a binary64 number. In fact, most decimal numbers with digits to the right of the decimal point lack an exact binary64 equivalent. For example, if you have a database column that is intended to hold an item price in dollars and cents, the only cents value that can be exactly represented are 0.00, 0.25, 0.50, and 0.75. Any other numbers to the right of the decimal point result in an approximation. If you provide a "price" value of 47.49, that number will be represented in binary64 as:

6683623321994527 × 2-47

Which works out to be:

47.49000000000000198951966012828052043914794921875

That number is very close to 47.49, but it is not exact. It is a little too big. If we reduce M by one to 6683623321994526 so that we have the next smaller possible binary64 value, we get:

47.4899999999999948840923025272786617279052734375

This second number is too small. The first number is closer to the desired value of 47.49, so that is the one that gets used. But it is not exact. Most decimal values work this way in IEEE 754. Remember the key point we made above:

Floating point values are approximate.

If you remember nothing else about floating-point values, please don't forget this one key idea.

1.2.2. Is it close enough?

The precision provided by IEEE 754 Binary64 is sufficient for most computations. For example, if "47.49" represents a price and inflation is running at 2% per year, then the price is going up by about 0.0000000301 dollars per second. The error in the recorded value of 47.49 represents about 66 nanoseconds worth of inflation. So if the 47.49 price is exact when you enter it, then the effects of inflation will cause the true value to exactly equal the value actually stored (47.4900000000000019895196601282805204391479492187) in less than one ten-millionth of a second. Surely that level of precision is sufficient for most purposes?

2. Extensions For Dealing With Floating Point Numbers

2.1. The ieee754.c Extension

The ieee754 extension converts a floating point number between its binary64 representation and the M×2E format. In other words in the expression:

F = M × 2E

The ieee754 extension converts between F and (M,E) and back again.

The ieee754 extension is not part of the amalgamation, but it is included by default in the CLI. If you want to include the ieee754 extension in your application, you will need to compile and load it separately.

2.1.1. The ieee754() function

The ieee754(F) SQL function takes a single floating-point argument as its input and returns a string that looks like this:

'ieee754(M,E)'

Except that the M and E are replaced by the mantissa and exponent of the floating point number. For example:

sqlite> .mode box
sqlite> SELECT ieee754(47.49) AS x;
┌───────────────────────────────┐
│               x               │
├───────────────────────────────┤
│ ieee754(6683623321994527,-47) │
└───────────────────────────────┘

Going in the other direction, the 2-argument version of ieee754() takes the M and E values and converts them into the corresponding F value:

sqlite> select ieee754(6683623321994527,-47) as x;
┌───────┐
│   x   │
├───────┤
│ 47.49 │
└───────┘

2.1.2. The ieee754_mantissa() and ieee754_exponent() functions

The text output of the one-argument form of ieee754() is great for human readability, but it is awkward to use as part of a larger expression. Hence the ieee754_mantissa() and ieee754_exponent() routines were added to return the M and E values corresponding to their single argument F value. For example:

sqlite> .mode box
sqlite> SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;
┌──────────────────┬─────┐
│        M         │  E  │
├──────────────────┼─────┤
│ 6683623321994527 │ -47 │
└──────────────────┴─────┘

2.1.3. The ieee754_from_blob() and ieee754_to_blob() functions

The ieee754_to_blob(F) SQL function converts the floating point number F into an 8-byte BLOB that is the big-endian binary64 encoding of that number. The ieee754_from_blob(B) function goes the other way, converting an 8-byte blob into the floating-point value that the binary64 encoding represents.

So, for example, if you read on Wikipedia that the encoding for the minimum positive binary64 value is 0x0000000000000001, then you can find the corresponding floating point value like this:

sqlite> .mode box
sqlite> SELECT ieee754_from_blob(x'0000000000000001') AS F;
┌───────────────────────┐
│           F           │
├───────────────────────┤
│ 4.94065645841247e-324 │
└───────────────────────┘

Or go the other way:

sqlite> .mode box
sqlite> SELECT quote(ieee754_to_blob(4.94065645841247e-324)) AS binary64;
┌─────────────────────┐
│      binary64       │
├─────────────────────┤
│ X'0000000000000001' │
└─────────────────────┘

2.2. The decimal.c Extension

The decimal extension provides arbitrary-precision decimal arithmetic on numbers stored as text strings. Because the numbers are stored to arbitrary precision and as text, no approximations are needed. Computations can be done exactly.

The decimal extension is not (currently) part of the SQLite amalgamation. However, it is included in the CLI. The source code to this extension can be found at ext/misc/decimal.c.

The decimal extension supports the following SQL functions and collating sequences:

2.2.1. The decimal_add(A,B), decimal_sub(A,B), and decimal_mul(A,B) functions

These functions compute the sum, difference, and product, respectively, of the two inputs A and B and return the result as text. The inputs can be either decimal text or numeric values. Numeric inputs are converted into decimal text before the computation is performed.

There is no "decimal_div(A,B)" function because division does not always have a finite-length decimal result.

2.2.2. The decimal_pow2(N) function

The decimal_pow2(N) function computes the exact decimal representation of the N-th power of 2. N must be an integer between -20000 and +20000.

This function can be slow and can use a lot of memory for large values of N.

2.2.3. The decimal(X) and decimal_exp(X) functions

The decimal(X) and decimal_exp(X) generate a decimal representation for input X. The input X can be an integer or a floating point number, or a text decimal. The decimal_exp(X) function returns the result in exponential notation (with a "e+NN" at the end) and decimal(X) returns a pure decimal (without the "e+NN").

If the input X is a floating point value, it is expanded to its exact decimal equivalent. For example:

sqlite> .mode qbox
sqlite> select decimal(47.49);
┌──────────────────────────────────────────────────────┐
│                    decimal(47.49)                    │
├──────────────────────────────────────────────────────┤
│ '47.49000000000000198951966012828052043914794921875' │
└──────────────────────────────────────────────────────┘

2.2.4. The decimal_cmp(X) function

The decimal_cmp(A,B) function compares two decimal values A and B. The result will be negative, zero, or positive if A is less than, equal to, or greater than B, respectively.

2.2.5. The decimal_sum(X) aggregate function

The decimal_sum(X) function is an aggregate, like the built-in sum() aggregate function, except that decimal_sum() computes its result to arbitrary precision and is therefore precise.

2.2.6. The decimal collating sequence

The decimal extension provides the "decimal" collating sequence that compares decimal text strings in numeric order.

百度