Javascript的數學難題

首先先考大家幾個數學題,如果這幾題的答案你都能順利的回答,那恭喜你!你可以安心地閱讀這篇文章,你一定能看懂!

題目ㄧ. 0.1 + 0.2 = ?
題目二. 1 – 0.9 = ?
題目三. 0.69 x 10 = ?

相信上面三個簡單的十進位數學運算,你的答案應該會跟我一樣吧?

第一題. 0.3
第二題. 0.1
第三題. 6.9

上面應該是無庸置疑的答案,但是如果我們把這三個題目丟給Javascript去運算之後,得到的結果會變成這樣:

第一題. 0.1 + 0.2 = 0.30000000000000004
第二題. 1 – 0.9 = 0.09999999999999998
第三題. 0.69 x 10 = 6.8999999999999995

什麼!?Javascript連這種小學生的算術都會算錯嗎?其實是有原因的,Javascript所有數字都是以64位浮點數形式儲存,也就是所有的數字都會變轉換成二進位。

上面題目中特別有提到這是3個十進位的數學運算,這也就是這個題目的重點,如果今天的題目改成:10 / 3 = 3.3333333333333334 也許你就能比較明白了,也就是說在十進位中要表達 1 / 3 和二進位中表達 1 / 10 一樣困難。

但其實在我們設計網頁的過程中,使用者是沒有二進位觀念的,只在乎運算出來的數值是否符合十進位的結果,這個時候我們可以利用一些技巧來達成正確的結果。

方法1 . 將小數變成整數後再進行運算


第一題. ((0.1 x 10) + (0.2 x 10)) / 10 = 0.3
第二題. ((1 x 10) – (0.9 x 10)) / 10 = 0.1
第三題. ((0.69 x 100) x 10 / 100) = 6.9

雖說以上的方法能夠順利完成任務,但若運算的數值是動態(Dynamic)的,乘數的部分就比較難下,所以我們可以採用第二種方法。

方法2. Math.js(https://mathjs.org/


透過網站上提供的js,或是透過CDN(https://cdnjs.com/libraries/mathjs)來引入後,透過以下的方法,就可以正確計算出上面三個題目的結果,其中14指的是精準度,通常已經很夠用了。

第一題. math.format(0.1 + 0.2 , 14) = 0.3
第一題. math.format(1 – 0.9 , 14) = 0.1
第一題. math.format(0.69 x 10 , 14) = 6.9

其實Math.js還有很多好用的功能,可以到它官方的網站閱讀一些文章,基本上所有的數學運算應該都可以透過它來玩完成。

You may also like...

107,505 Responses

  1. From the next day, our precise trek started from a place called Nayapul the place we reached in about 2 hours.

  2. DarrenWab表示:

    gramster.ru gramster пин ап зеркало

  3. EarnestAvada表示:

    This text is priceless. When can I find out more?

    https://esco-center.com.ua/stil-i-funkcionalnost-s-bi-led-linzami

  4. Lhaneevige表示:

    Hmm is anyone else having problems with the images on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.

    https://elsig-opt.com.ua/budushchee-avtomobilnyh-far-pochemu-bi-led-linzy-novyi-standart

  5. Lhaneevige表示:

    What’s up, everything is going well here and ofcourse every one is sharing data, that’s in fact fine, keep up writing.

    http://infobus.com.ua/h7-lens-guide.html

  6. Lewisslund表示:

    Admiring the time and energy you put into your website and in depth information you offer. It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

    https://eximp.com.ua/komplekt-bi-led-linz-i-komponentov-dlya-idealnyh-far

  7. RobertCow表示:

    gramster.ru: Gramster – пин ап зеркало

  8. If we wish to crawl away from our high-definition Tv sets and get out the home, the upper snack costs could have to stay for now.

  9. GichardPheve表示:

    Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a little bit, but other than that, this is excellent blog. An excellent read. I’ll definitely be back.

    https://prego.in.ua/kak-vosstanovit-fary-s-novym-steklom

  10. Lewisslund表示:

    I blog frequently and I truly thank you for your information. Your article has really peaked my interest. I’m going to book mark your website and keep checking for new information about once per week. I subscribed to your Feed as well.

    https://3dlevsha.com.ua/top-proizvoditelej-stekla-dlya-far-avto-v-2024-godu

  11. Ismaelfer表示:

    This is very attention-grabbing, You are an excessively professional blogger. I have joined your feed and stay up for in search of extra of your excellent post. Additionally, I have shared your web site in my social networks

    https://yurchakbeauty.com.ua/kak-vybrat-steklo-i-korpusa-dlya-far-vash%D0%B5%D0%B3%D0%BE-avtomobilya-polnoe-rukovodstvo

  12. Felix’s Swim Schools has been a reliable leader in swimming instruction across Toronto and the Greater Toronto Area for more than forty years. Whether it’s newborns and toddlers splashing their first or adults perfecting their strokes or working toward advanced certifications, we take great satisfaction in providing top-notch swimming lessons catered to every swimmer’s particular needs.
    Three basic ideas form the cornerstone of our programs: aquatic safety, progressive learning, and personalized training. Small class sizes, a friendly environment, and highly qualified teachers help us to make sure every swimmer gets the concentrated attention and support required to flourish. At Felix’s, swimming is more than simply a talent; it’s a life-saving ability, a confidence builder, and a road toward personal development and happiness.

  13. RobertCow表示:

    gramster.ru: gramster – pinup 2025

  14. GichardPheve表示:

    Greate post. Keep writing such kind of info on your site. Im really impressed by your site.
    Hey there, You have performed an incredible job. I’ll definitely digg it and personally suggest to my friends. I’m sure they will be benefited from this site.

    https://dvernoyolimp.org.ua/led-lampy-dlya-far-idealnyy-vybor-dlya-sovremennogo-avto

  15. WilliamDen表示:

    http://gramster.ru/# gramster.ru

  16. FobertWen表示:

    This is my first time pay a visit at here and i am actually pleassant to read everthing at alone place.

    https://hr.com.ua/energoeffektivnye-led-lampy-dlya-avto-dolgov%D0%B5%D1%87nost-i-moschnost

  17. GichardPheve表示:

    Thanks for another fantastic article. The place else could anyone get that type of information in such an ideal means of writing? I have a presentation next week, and I am at the search for such information.

    https://decorator.net.ua/kogda-menyat-steklo-far-priznaki-iznosa-i-sovety-po-uhodu

  18. FobertWen表示:

    Hi, I would like to subscribe for this blog to get latest updates, therefore where can i do it please help out.

    http://sun-shop.com.ua/bilead-lens-price-comparison.html

  19. Jeffreywex表示:

    http://gramster.ru/# пин ап

  20. FobertWen表示:

    At this time it seems like BlogEngine is the best blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?

    https://jiraf.com.ua/moshhnoe-tochnoe-osveshhenie-preimushhestva-linz-dlya-avto-far

  21. Jeffreywex表示:

    http://gramster.ru/# pinup 2025

  22. Jeffreywex表示:

    https://gramster.ru/# pinup 2025

  23. Character disorders can manifest in varied methods, making it vital to know the precise signs associated with several types of persona disorders.

  24. DarrenWab表示:

    gramster.ru Gramster pinup 2025

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。