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還有很多好用的功能,可以到它官方的網站閱讀一些文章,基本上所有的數學運算應該都可以透過它來玩完成。
https://rybelsus.shop/# semaglutide cost
Профессиональный сервисный центр по ремонту сетевых хранилищ в Москве.
Мы предлагаем: надежный сервис ремонта сетевых хранилищ
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
ozempic cost: ozempic – buy cheap ozempic
best betting sites bonuses: A promo code for the 1xBet platform, which offers various bonuses like deposit matches, free bets, or free spins in the casino section. It is often used by new users during sign-up.
https://rybelsus.shop/# semaglutide cost
Веселые анекдоты и шутки
Подними себе настроение
semaglutide cost: buy semaglutide pills – rybelsus coupon
ozempic online ozempic cost ozempic cost
buy semaglutide online: rybelsus cost – rybelsus price
http://rybelsus.shop/# semaglutide online
https://rybelsus.shop/# rybelsus coupon
ремонт стекол ремонт лобового стекла
https://rybelsus.shop/# rybelsus pill
Смешные свежие анекдоты и шутки
Отдохни и посмейся
Профессиональный сервисный центр по ремонту сигвеев в Москве.
Мы предлагаем: ремонт сигвеев в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
ozempic online: ozempic coupon – buy ozempic
Лучшие свежие анекдоты и шутки
Заходи посмеяться
Профессиональный сервисный центр по ремонту сетевых хранилищ в Москве.
Мы предлагаем: цены на ремонт сетевых хранилищ
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
https://ozempic.art/# Ozempic without insurance
Ozempic without insurance Ozempic without insurance buy ozempic
buy cheap ozempic ozempic online ozempic online
ozempic online: buy ozempic pills online – buy cheap ozempic
order permethrin without prescription – benzoyl peroxide for sale buy retin paypal
Лучшие анекдоты и шутки
Заходи посмеяться
rybelsus coupon: semaglutide online – semaglutide tablets
Hong Kong plans to install thousands of surveillance cameras. Critics say it’s more proof the city is moving closer to China
услуги адвоката по гражданским делам
Glance up while strolling through parts of downtown Hong Kong and, chances are, you’ll notice the glassy black lens of a surveillance camera trained on the city’s crowded streets.
And that sight will become more common in the coming years, as the city’s police pursue an ambitious campaign to install thousands of cameras to elevate their surveillance capabilities.
Though it consistently ranks among the world’s safest big cities, police in the Asian financial hub say the new cameras are needed to fight crime – and have raised the possibility of equipping them with powerful facial recognition and artificial intelligence tools.
That’s sparked alarm among some experts who see it as taking Hong Kong one step closer to the pervasive surveillance systems of mainland China, warning of the technology’s repressive potential.
Hong Kong police had previously set a target of installing 2,000 new surveillance cameras this year, and potentially more than that each subsequent year. The force plans to eventually introduce facial recognition to these cameras, security chief Chris Tang told local media in July – adding that police could use AI in the future to track down suspects.
In a statement to CNN, the Hong Kong Police Force said it was studying how police in other countries use surveillance cameras, including how they use AI. But it’s not clear how many of the new cameras may have facial recognition capabilities, or whether there’s a timeline for when the tech will be introduced.
Tang and the Hong Kong police have repeatedly pointed to other jurisdictions, including Western democracies, that also make wide use of surveillance cameras for law enforcement. For instance, Singapore has 90,000 cameras and the United Kingdom has more than seven million, Tang told local newspaper Sing Tao Daily in June.
While some of those places, like the UK, have started using facial recognition cameras, experts say these early experiments have highlighted the need for careful regulation and privacy protections. Hong Kong police told CNN they would “comply with relevant laws” and follow strong internal guidelines – but haven’t elaborated in depth on what that would look like.
http://ozempic.art/# buy ozempic
ozempic coupon ozempic online buy cheap ozempic
https://rybelsus.shop/# rybelsus price
https://rybelsus.shop/# rybelsus coupon