2 + 2 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a Javascript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
Javascript 权威指南第 5 版
Closure: The Definitive Guide
PeacePeach
V2EX    Javascript

2 + 2

  •  
  •   PeacePeach 2021-05-15 22:24:19 +08:00 4084 次点击
    这是一个创建于 1629 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Console
    > 2 + 2
    > 4
    > "2" + "2"
    > "22"
    > 2 + 2 - 2
    > 2
    > "2" + "2" - "2"
    > 20
    22 条回复    2021-05-17 19:41:06 +08:00
    pabupa
        1
    pabupa  
       2021-05-15 22:37:00 +08:00
    iOCZ
        2
    iOCZ  
       2021-05-15 22:51:06 +08:00
    +字符串优先,-只能数字
    kindjeff
        3
    kindjeff  
       2021-05-15 22:54:37 +08:00   19
    xiri
        4
    xiri  
       2021-05-16 02:34:40 +08:00   1
    优秀的语言
    SilencerL
        5
    SilencerL  
       2021-05-16 02:40:45 +08:00 via iPhone   2
    https://v2c.tech/Article/121
    当初我还用神奇的 JS 这个梗做了几张 PPT
    GG668v26Fd55CP5W
        6
    GG668v26Fd55CP5W  
       2021-05-16 02:45:42 +08:00 via iPhone   1
    it′s not a bug , it is a feature .
    LeeReamond
        7
    LeeReamond  
       2021-05-16 03:51:20 +08:00 via Android
    谢谢你,Javascript!
    Pastsong
        8
    Pastsong  
       2021-05-16 07:59:35 +08:00
    这些都是 ECMAScript language specification 的预定义行为,也不奇怪吧
    Perry
        9
    Perry  
       2021-05-16 08:18:01 +08:00 via iPhone
    同意楼上,都是按照 specification 来的东西。谁说一个语言的执行结果一定要按照你的直觉来?
    hello2060
        10
    hello2060  
       2021-05-16 08:24:49 +08:00
    多大点事啊, 就连我这种 java 程序员都知道 js 的这几个幺蛾子
    guanghua812
        11
    guanghua812  
       2021-05-16 08:46:28 +08:00
    "+"字符串拼接,"-"做运算
    statement
        12
    statement  
       2021-05-16 08:49:14 +08:00 via iPhone
    我都是-0 然后再加
    TonyG
        13
    TonyG  
       2021-05-16 10:00:10 +08:00
    var name = 1
    name + 1
    let name = 1
    name + 1
    Biwood
        14
    Biwood  
       2021-05-16 10:04:58 +08:00 via iPhone   1
    litel
        15
    litel  
       2021-05-16 11:54:55 +08:00
    > 100*0.014
    < 1.4000000000000001
    GM
        16
    GM  
       2021-05-16 12:06:16 +08:00
    这样一个辣鸡语言,居然成了他妈的最流行的语言,这个世界到底是怎么了。
    charlie21
        17
    charlie21  
       2021-05-16 12:16:51 +08:00   1
    Binary floating point math is like THIS. In most programming languages, it is based on the IEEE 754 standard.
    所以,一切要从说起 IEEE 754 standard 说起、从二进制电脑如何模拟十进制运算说起

    * Is floating point math broken?
    https://stackoverflow.com/questions/588004/is-floating-point-math-broken

    Javascript treats decimals as floating point numbers, which means operations like addition might be subject to rounding error. You might want to take a look at this article: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Just for information, ALL numeric types in Javascript are IEEE-754 Doubles.

    Javascript uses the IEEE 754 standard for Math, it makes use of 64-bit floating numbers. This causes precision errors when doing floating point (decimal) calculations, in short, due to computers working in Base 2 while decimal is Base 10.

    In most programming languages, it is based on the IEEE 754 standard.
    Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard.


    P.S.
    位数问题,math broken 情况,数字精度问题最终是位数问题

    C# 是有通过专门的 decimal 数据类型作为值类型来解决这个问题的(),搜 C# do math float or decimal

    https://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise

    C# float only has 7 digit precision, and 99.999999f has 8 digits.
    A single-precision IEEE-754 float will only be 32-bits, which gives around 7 decimal digits of precision. If you want better than that, use a double.

    C# 是有通过专门的 decimal 数据类型作为值类型来解决这个问题的,搜 IEEE-754 vs C# decimal

    https://stackoverflow.com/questions/9079225/decimal-type-in-c-sharp-vs-ieee-754-standard

    C# decimal doesn't follow the same rules as IEEE 754 floating point numbers. The C# 4 specification is quite clear on how it should behave ... 别的语言应该有类似的库

    关于 C# decimal 数据类型本身的弱点:
    as far as I know, decimal is not compatible with anything - it's a Microsoft only invention - so you can't think of it in the same terms as normal floating point stuff. And it doesn't have subnormal values.

    P.S.
    你看到的控制台输出怪异结果(或者编程语言的怪异行为)是一个根据 IEEE-754 作浮点数运算的计算机语言如何处理浮点数的正确方式。别大惊小怪了
    xingguang
        19
    xingguang  
       2021-05-17 16:02:37 +08:00
    @GM 世界上有两种语言:没人用的语言和一只被人骂的语言
    AV1
        20
    AV1  
       2021-05-17 16:35:15 +08:00
    JS 月经贴。

    尤其是 0.1+0.2 这个问题,无非是 JS 更出名更容易欺负罢了。
    AV1
        21
    AV1  
       2021-05-17 16:40:54 +08:00
    @aragakiyuii

    我试了下 python 类似的操作:
    print('python is shit!' if 0.1 + 0.2 != 0.3 else 'python is awesome!')
    print('python is shit!' if True+True+True == 3 else 'python is awesome!')
    print('python is shit!' if True-True == 0 else 'python is awesome!')
    print('python is shit!' if True == 1 and not(True is 1) else 'python is awesome!')

    emm……
    drydiy
        22
    drydiy  
       2021-05-17 19:41:06 +08:00
    JS 月经贴
    + 字符串优先级高。- 都转成数字类型运算。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2749 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 11:01 PVG 19:01 LAX 04:01 JFK 07:01
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86