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

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

Elvis Shi
December 15, 2021

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

Elvis Shi

December 15, 2021
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

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

    View full-size slide

  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 {

    View full-size slide

  3. ʲ଎ใʳΏΊΈɺ҉߸௨՟Λൃߦʂ
    ˞ӕͰ͢ɻ

    View full-size slide

  4. ͱ͍͏Θ͚Ͱ

    ΏΊΈͷڅ༩͸:VNF$PJOͰࢧڅʹ
    ˞ӕͰ͢ɻ

    View full-size slide

  5. // ࠓ݄ͷڅ༩


    var myYumeCoin = 0.2


    // ࠓ݄ͷϘʔφε


    myYumeCoin += 0.1


    // ϘʔφεΛ࢖ͬͯങ͍෺ʂ


    myYumeCoin -= 0.1


    // ࢒ߴ֬ೝ


    print(myYumeCoin) // 0.20000000000000004 ??????

    View full-size slide

  6. ͳͥ͜Μͳ͜ͱ͕ى͜Δͷ͔
    w 4XJGUͰখ਺Λѻ͏ͱ͖ɺσϑΥϧτͰ͸Double͕
    ࢖ΘΕΔ
    w Double͸ʮഒਫ਼౓ුಈখ਺఺਺ʯͳͷͰɺਫ਼౓ͷ໰
    ୊Ͱͦ΋ͦ΋ਖ਼֬ʹදͤͳ͍਺ࣈ͕ͨ͘͞Μ͋Δ
    w େ఍ͷ৔߹͸ͦͷؙΊΒΕͨ਺ࣈͰ΋໰୊ʹͳΒͳ͍
    ͕ɺۚ༥ܥͳͲͷ৔߹͸ਖ਼֬ʹѻ͏ඞཁ͕͋Δ

    View full-size slide

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

    View full-size slide

  8. // ࠓ݄ͷڅ༩


    var myYumeCoin = 0.2


    // ࠓ݄ͷϘʔφε


    myYumeCoin += 0.1


    // ϘʔφεΛ࢖ͬͯങ͍෺ʂ


    myYumeCoin -= 0.1


    // ࢒ߴ֬ೝ


    print(myYumeCoin) // 0.20000000000000004 ??????

    View full-size slide

  9. import Foundation


    // ࠓ݄ͷڅ༩


    var myYumeCoin: Decimal = 0.2


    // ࠓ݄ͷϘʔφε


    myYumeCoin += 0.1


    // ϘʔφεΛ࢖ͬͯങ͍෺ʂ


    myYumeCoin -= 0.1


    // ࢒ߴ֬ೝ


    print(myYumeCoin) // 0.2 🎉
    'PVOEBUJPO͕ඞཁ
    ܕΛ໌ࣔతʹ%FDJNBMʹࢦఆ

    View full-size slide

  10. DecimalɺŜƃŤƄƃŬžŜŘŢŦ

    View full-size slide

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

    View full-size slide

  12. import Foundation


    // ࠓ݄ͷڅ༩


    var myYumeCoin: Decimal = 0.2


    // ࠓ݄ͷϘʔφε


    myYumeCoin += 0.1


    // ϘʔφεΛ࢖ͬͯങ͍෺ʂ


    myYumeCoin -= 0.1


    // ࢒ߴ֬ೝ


    print(myYumeCoin) // 0.2 🎉

    View full-size slide

  13. import Foundation


    // ࠓ݄ͷڅ༩


    var myYumeCoin: Decimal = 0.2


    // ࠓ݄ͷϘʔφε


    myYumeCoin += 0.1


    // ϘʔφεΛ࢖ͬͯങ͍෺ʂ


    myYumeCoin -= 0.1


    // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ


    myYumeCoin += 123.456


    // ࢒ߴ֬ೝ


    print(myYumeCoin) // 123.65599999999997952 !!!?????

    View full-size slide

  14. DecimalɺūŬŹƂŜŽūŘ

    View full-size slide

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

    View full-size slide

  16. খ਺ϦςϥϧͰ

    DecimalΛ୅ೖ͠ͳ͍Ͱʂ

    View full-size slide

  17. %FDJNBMΛखܰʢʁʣʹѻ͏ͨΊͷ

    ΠχγϟϥΠβʔ
    • init(_ value: String, format: Decimal.FormatStyle,
    lenient: Bool = true) throws // iOS 15+


    • init?(string: String, locale: Locale? = nil) // iOS 7+


    • // etc…

    View full-size slide

  18. import Foundation


    // ࠓ݄ͷڅ༩


    var myYumeCoin: Decimal = 0.2


    // ࠓ݄ͷϘʔφε


    myYumeCoin += 0.1


    // ϘʔφεΛ࢖ͬͯങ͍෺ʂ


    myYumeCoin -= 0.1


    // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ


    myYumeCoin += 123.456


    // ࢒ߴ֬ೝ


    print(myYumeCoin) // 123.65599999999997952 !!!?????

    View full-size slide

  19. import Foundation


    // …


    // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ


    //myYumeCoin += 123.456


    // ࠓ౓ͦ͜ʂ


    myYumeCoin += try! .init("123.456", format: .number)


    // ࢒ߴ֬ೝ


    print(myYumeCoin) // 123.656 🎉

    View full-size slide

  20. DecimalɺͪΐͬͱͰ͖͔ͨͳ

    View full-size slide

  21. IUUQTGPSVNTTXJGUPSHUIPXUPJOJUJBMJ[FEFDJNBM

    View full-size slide

  22. IUUQTJEFBMSFBMJUZDPNQSPHSBNJOHTXJGUEFDJNBMTUSJOH

    View full-size slide

  23. IUUQTZVNFNJDPOOQBTTDPNFWFOU

    View full-size slide

  24. IUUQTXXXZVNFNJDPKQDBUFHPSZSFDSVJU

    View full-size slide