Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Decimal、正しく使ってる? / Are you using Decimal correc...
Search
Elvis Shi
December 15, 2021
Programming
4
660
Decimal、正しく使ってる? / Are you using Decimal correctly?
Elvis Shi
December 15, 2021
Tweet
Share
More Decks by Elvis Shi
See All by Elvis Shi
How did I build an Open-Source SwiftUI Toast Library
lovee
1
26
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
500
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
230
Observation のあれこれ / A brief introduction about Observation
lovee
3
330
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
26
8.1k
属人化しない為の勉強会作り / To make tech meetups with less personal dependencies
lovee
0
250
偏見と妄想で語るスクリプト言語としての Swift / Swift as a Scripting Language
lovee
2
750
danger-swift-kantoku
lovee
1
460
A story about me trying to make a router that manages when, how and which view to transit in a SwiftUI app
lovee
1
380
Other Decks in Programming
See All in Programming
Hono・Prisma・AWSでGeoなAPI開発
nokonoko1203
5
640
2024 컴포즈 정원사
jisungbin
0
150
サーバーレスで負荷試験!Step Functions + Lambdaを使ったk6の分散実行
shuntakahashi
2
410
Go Code Generation at newmo / 2024-08-27 #newmo_layerx_go
genkey6
0
540
Rubyのobject_id
qnighy
6
1.3k
プログラマのための音楽入門
cheebow
5
560
開発を加速する共有Swift Package実践
elmetal
PRO
0
360
ドメイン駆動設計を実践するために必要なもの
bikisuke
3
310
What we keep in mind when migrating from Serverless Framework to AWS CDK and AWS SAM
kasacchiful
1
130
Mastering AsyncSequence - 使う・作る・他のデザインパターン(クロージャ、Delegate など)から移行する
treastrain
4
1.5k
今インフラ技術をイチから学び直すなら
yuhta28
1
120
現代のVueとTypeScript - 型安全の活用術
minako__ph
4
3.1k
Featured
See All Featured
Making Projects Easy
brettharned
113
5.8k
We Have a Design System, Now What?
morganepeng
48
7.1k
Facilitating Awesome Meetings
lara
49
5.9k
Clear Off the Table
cherdarchuk
90
320k
The Illustrated Children's Guide to Kubernetes
chrisshort
46
48k
4 Signs Your Business is Dying
shpigford
179
21k
Infographics Made Easy
chrislema
239
18k
Producing Creativity
orderedlist
PRO
340
39k
Rebuilding a faster, lazier Slack
samanthasiow
78
8.5k
Speed Design
sergeychernyshev
19
410
In The Pink: A Labor of Love
frogandcode
139
22k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.4k
Transcript
%FDJNBMɺਖ਼ͬͯ͘͠Δʁ forJ1IPOF%FW4BQQPSPGFBU)","5"TXJGU
} var employedBy = "YUMEMI Inc." var job = "iOS
Tech Lead" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ M1 Max MacBook Proࠓಧ͍ͨʂ """ final class Me: Developable, Talkable {
ʲใʳΏΊΈɺ҉߸௨՟Λൃߦʂ ˞ӕͰ͢ɻ
ͱ͍͏Θ͚Ͱ ΏΊΈͷڅ༩:VNF$PJOͰࢧڅʹ ˞ӕͰ͢ɻ
// ࠓ݄ͷڅ༩ var myYumeCoin = 0.2 // ࠓ݄ͷϘʔφε myYumeCoin +=
0.1 // ϘʔφεΛͬͯങ͍ʂ myYumeCoin -= 0.1 // ߴ֬ೝ print(myYumeCoin) // 0.20000000000000004 ??????
ͳͥ͜Μͳ͜ͱ͕ى͜Δͷ͔ w 4XJGUͰখΛѻ͏ͱ͖ɺσϑΥϧτͰDouble͕ ΘΕΔ w Doubleʮഒਫ਼ුಈখʯͳͷͰɺਫ਼ͷ Ͱͦͦਖ਼֬ʹදͤͳ͍ࣈ͕ͨ͘͞Μ͋Δ w େͷ߹ͦͷؙΊΒΕͨࣈͰʹͳΒͳ͍ ͕ɺۚ༥ܥͳͲͷ߹ਖ਼֬ʹѻ͏ඞཁ͕͋Δ
Decimal
Decimalܕ w ුಈখΛेਐϕʔεͰѻ͏ͨΊͷܕ w ༗ޮܻ͕ܻ w ༗ޮܻൣғͰ͋Εेਐදهͷޡ͕ࠩͳ͍
// ࠓ݄ͷڅ༩ var myYumeCoin = 0.2 // ࠓ݄ͷϘʔφε myYumeCoin +=
0.1 // ϘʔφεΛͬͯങ͍ʂ myYumeCoin -= 0.1 // ߴ֬ೝ print(myYumeCoin) // 0.20000000000000004 ??????
import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //
ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛͬͯങ͍ʂ myYumeCoin -= 0.1 // ߴ֬ೝ print(myYumeCoin) // 0.2 🎉 'PVOEBUJPO͕ඞཁ ܕΛ໌ࣔతʹ%FDJNBMʹࢦఆ
DecimalɺŜƃŤƄƃŬžŜŘŢŦ
DecimalɺŜƃŤƄƃŬžŜŘŢŦ ͱɺࢥͬͨʁ
import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //
ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛͬͯങ͍ʂ myYumeCoin -= 0.1 // ߴ֬ೝ print(myYumeCoin) // 0.2 🎉
import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //
ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛͬͯങ͍ʂ myYumeCoin -= 0.1 // Decimal Λͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑Ϙʔφεʂ myYumeCoin += 123.456 // ߴ֬ೝ print(myYumeCoin) // 123.65599999999997952 !!!?????
DecimalɺūŬŹƂŜŽūŘ
DecimalͷϦςϥϧೖ w খϦςϥϧͰDecimalΛೖͰ͖Δͷɺ ExpressibleByFloatLiteralΛར༻ͨ݁͠Ռ w ࣮init(floatLiteral value: Double) w DecimalΛ࡞ΔલʹͦͦઌʹҰճDoubleΛ
࡞ͬͯͦͷ݁ՌΛೖ͍ͯ͠Δ͚ͩʂ
খϦςϥϧͰ DecimalΛೖ͠ͳ͍Ͱʂ
%FDJNBMΛखܰʢʁʣʹѻ͏ͨΊͷ ΠχγϟϥΠβʔ • init(_ value: String, format: Decimal.FormatStyle, lenient:
Bool = true) throws // iOS 15+ • init?(string: String, locale: Locale? = nil) // iOS 7+ • // etc…
import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //
ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛͬͯങ͍ʂ myYumeCoin -= 0.1 // Decimal Λͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑Ϙʔφεʂ myYumeCoin += 123.456 // ߴ֬ೝ print(myYumeCoin) // 123.65599999999997952 !!!?????
import Foundation // … // Decimal Λͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑Ϙʔφεʂ //myYumeCoin += 123.456
// ࠓͦ͜ʂ myYumeCoin += try! .init("123.456", format: .number) // ߴ֬ೝ print(myYumeCoin) // 123.656 🎉
DecimalɺͪΐͬͱͰ͖͔ͨͳ
ʲࢀߟʳ
IUUQTGPSVNTTXJGUPSHUIPXUPJOJUJBMJ[FEFDJNBM
IUUQTJEFBMSFBMJUZDPNQSPHSBNJOHTXJGUEFDJNBMTUSJOH
ʲએʳ
IUUQTZVNFNJDPOOQBTTDPNFWFOU
IUUQTXXXZVNFNJDPKQDBUFHPSZSFDSVJU