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還有很多好用的功能,可以到它官方的網站閱讀一些文章,基本上所有的數學運算應該都可以透過它來玩完成。
пин ап Gramster пин ап вход
pinup 2025 gramster.ru пин ап казино
An individual who is keen to get a job in the hospitality sector should have enough data and expertise and may have proper expertise in order that one can sustain at the place for a longer time frame.
Dark web Crypto Exchange – Withdraw bitcoin with negative AML, Helped solve the problem with the negative aml of bitcoin
Founded in 1990, the ecomuseum consists of a coordinated assemblage of open-air itineraries with museums and teaching centers, inside the Pistoian Mountain Area of the Northern Apennines.
снятие ломок [url=https://uaforum.ukrbb.net/viewtopic.php?f=13&t=3260]снятие ломок[/url] .
снятие ломки клинике снятие ломки клинике .
A princess who experienced tragedy at a younger age, this character was additionally in hazard of losing another family member.
Как приобрести диплом техникума с минимальными рисками
The Neorest takes the cake as probably the most costly commode obtainable for purchase in the house.
http://gramster.ru/# pinup 2025
Even if you’re travelling with a low funds then also you will not came across any trouble in visiting Cambodia.
myspace.com https://ucgp.jujuy.edu.ar/profile/rangebrian0/ .
myspace.com http://todd-bray.mdwrite.net/pornography-as-a-tool-for-improving-sexual-communication-and-intimacy-1733893128/ .
myspace.com http://it-viking.ch/index.php/Pornography_And_The_Breaking_Down_Of_Sexual_Taboos_And_Stigmas .
https://gramster.ru/# gramster.ru
https://gramster.ru/# gramster.ru
https://gramster.ru/# пин ап казино
пин ап казино: gramster.ru – пин ап вход
It’s amazing to visit this web page and reading the views of all colleagues on the topic of this piece of writing, while I am also keen of getting familiarity.
https://piar.org.ua/kak-sokhranit-steklo-far-v-idealnom-sostoyanii-profilaktika-i-ukhod
WPS Office: 一站式办公服务平台: 新升级,无广告,AI办公更高效. 立即下载. 登录使用. WPS 365: 面向组织和企业的WPS 365: 一站式AI办公,生产力即刻起飞. 了解更多. 咨询,记忆体占用低,体积轻运行快. 将文字、表格、演示、PDF等融合为一个组件。WPS下载 https://www.wpsue.com
myspace.com http://www.rhythmgamingworld.com/members/alarmspoon8/activity/1744752/ .
myspace.com https://justpaste.me/M25c .
снятие ломки нарколог снятие ломки нарколог .
Hello colleagues, how is everything, and what you wish for to say on the topic of this article, in my view its really awesome in support of me.
https://kirovrada.dp.ua/yak-zaminyty-sklo-far-pokrokova-instrukciya-dlya-avtomobilistiv
http://gramster.ru/# пинап казино
http://gramster.ru/# пин ап казино
снятие ломки наркомана [url=https://www.uaforum.ukrbb.net/viewtopic.php?f=13&t=3260]снятие ломки наркомана[/url] .
I got this website from my friend who shared with me concerning this website and now this time I am browsing this site and reading very informative content at this place.
http://jiraf.com.ua/halogen-lens-guide.html
Thanks for the auspicious writeup. It if truth be told was a leisure account it. Glance complicated to more delivered agreeable from you! By the way, how could we communicate?
http://kvn-tehno.com.ua/3-inch-bilead-market-review.html