Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Decimal、正しく使ってる? / Are you using Decimal correctly?

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
December 15, 2021

Decimal、正しく使ってる? / Are you using Decimal correctly?

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

December 15, 2021
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. %FDJNBMɺਖ਼͘͠࢖ͬͯΔʁ forJ1IPOF%FW4BQQPSPGFBU)","5"TXJGU

  2. } 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 {
  3. ʲ଎ใʳΏΊΈɺ҉߸௨՟Λൃߦʂ ˞ӕͰ͢ɻ

  4. ͱ͍͏Θ͚Ͱ 
 ΏΊΈͷڅ༩͸:VNF$PJOͰࢧڅʹ ˞ӕͰ͢ɻ

  5. // ࠓ݄ͷڅ༩ var myYumeCoin = 0.2 // ࠓ݄ͷϘʔφε myYumeCoin +=

    0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.20000000000000004 ??????
  6. ͳͥ͜Μͳ͜ͱ͕ى͜Δͷ͔ w 4XJGUͰখ਺Λѻ͏ͱ͖ɺσϑΥϧτͰ͸Double͕ ࢖ΘΕΔ w Double͸ʮഒਫ਼౓ුಈখ਺఺਺ʯͳͷͰɺਫ਼౓ͷ໰ ୊Ͱͦ΋ͦ΋ਖ਼֬ʹදͤͳ͍਺ࣈ͕ͨ͘͞Μ͋Δ w େ఍ͷ৔߹͸ͦͷؙΊΒΕͨ਺ࣈͰ΋໰୊ʹͳΒͳ͍ ͕ɺۚ༥ܥͳͲͷ৔߹͸ਖ਼֬ʹѻ͏ඞཁ͕͋Δ

  7. Decimal

  8. Decimalܕ w ුಈখ਺఺਺Λेਐ਺ϕʔεͰѻ͏ͨΊͷܕ w ༗ޮܻ਺͕ܻ w ༗ޮܻ਺ൣғ಺Ͱ͋Ε͹ेਐ਺දهͷޡ͕ࠩͳ͍

  9. // ࠓ݄ͷڅ༩ var myYumeCoin = 0.2 // ࠓ݄ͷϘʔφε myYumeCoin +=

    0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.20000000000000004 ??????
  10. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.2 🎉 'PVOEBUJPO͕ඞཁ ܕΛ໌ࣔతʹ%FDJNBMʹࢦఆ
  11. DecimalɺŜƃŤƄƃŬžŜŘŢŦ

  12. DecimalɺŜƃŤƄƃŬžŜŘŢŦ ͱɺࢥͬͨʁ

  13. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.2 🎉
  14. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ myYumeCoin += 123.456 // ࢒ߴ֬ೝ print(myYumeCoin) // 123.65599999999997952 !!!?????
  15. DecimalɺūŬŹƂŜŽūŘ

  16. DecimalͷϦςϥϧ୅ೖ w খ਺ϦςϥϧͰDecimalΛ୅ೖͰ͖Δͷ͸ɺ ExpressibleByFloatLiteralΛར༻ͨ݁͠Ռ w ࣮૷͸init(floatLiteral value: Double) w DecimalΛ࡞Δલʹͦ΋ͦ΋ઌʹҰճDoubleΛ

    ࡞ͬͯͦͷ݁ՌΛ୅ೖ͍ͯ͠Δ͚ͩʂ
  17. খ਺ϦςϥϧͰ 
 DecimalΛ୅ೖ͠ͳ͍Ͱʂ

  18. %FDJNBMΛखܰʢʁʣʹѻ͏ͨΊͷ 
 ΠχγϟϥΠβʔ • init(_ value: String, format: Decimal.FormatStyle, lenient:

    Bool = true) throws // iOS 15+ • init?(string: String, locale: Locale? = nil) // iOS 7+ • // etc…
  19. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ myYumeCoin += 123.456 // ࢒ߴ֬ೝ print(myYumeCoin) // 123.65599999999997952 !!!?????
  20. import Foundation // … // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ //myYumeCoin += 123.456

    // ࠓ౓ͦ͜ʂ myYumeCoin += try! .init("123.456", format: .number) // ࢒ߴ֬ೝ print(myYumeCoin) // 123.656 🎉
  21. DecimalɺͪΐͬͱͰ͖͔ͨͳ

  22. ʲࢀߟʳ

  23. IUUQTGPSVNTTXJGUPSHUIPXUPJOJUJBMJ[FEFDJNBM

  24. IUUQTJEFBMSFBMJUZDPNQSPHSBNJOHTXJGUEFDJNBMTUSJOH

  25. ʲએ఻ʳ

  26. IUUQTZVNFNJDPOOQBTTDPNFWFOU

  27. IUUQTXXXZVNFNJDPKQDBUFHPSZSFDSVJU