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

SE-0246 Generic Math(s) Functions

SE-0246 Generic Math(s) Functions

Shigure Shimotori

April 10, 2019
Tweet

More Decks by Shigure Shimotori

Other Decks in Programming

Transcript

  1. SE-0246:

    GENERIC MATH(S) FUNCTIONS
    OTEMACHI.SWIFT #3

    View Slide

  2. SE-0246: GENERIC MATH(S) FUNCTIONS
    ࣗݾ঺հ
    ▸ S_Shimotori
    ▸ ඍੵ΋ઢܗ΋Ͱ͖ͳ͔ͬͨΑ…

    View Slide

  3. SE-0246:

    GENERIC MATH(S) FUNCTIONS

    View Slide

  4. View Slide

  5. ਺ֶͬΆ͍ܭࢉ͕
    ͍Ζ͍ΖͰ͖Δ༧ײʂʂʂʂ

    View Slide

  6. SE-0246: GENERIC MATH(S) FUNCTIONS
    ࠓ೔࿩͢͜ͱ
    ▸ SE-0246ͷ಺༰
    ▸ with modificationsͬͯͳʹ
    ▸ Ϟδϡʔϧ໊Ͳ͏͢Δ໰୊
    ▸ logؔ਺ͷ໊લͲ͏͢Δ໰୊

    View Slide

  7. SE-0246ͷ಺༰

    View Slide

  8. SE-0246: GENERIC MATH(S) FUNCTIONS
    SE-0246ͷϞνϕ
    ▸ C mathϥΠϒϥϦͷݺͼग़͕͠Πέͯͳ͍
    ▸ Darwin͔GNU C͔Ͱimportʹ෼ذ͕ൃੜ͢Δ
    ▸ protocol (Binary)FloatingPoint͕Πέͯͳ͍
    ▸ CGFloat, Double, Float, Float80 \in BinaryFloatingPoint
    ▸ ͜ͷprotocolΛ࢖ͬͯ਺஋ܭࢉΛߟ͑Δͷ͸ෆศ

    View Slide

  9. SE-0246: GENERIC MATH(S) FUNCTIONS
    SE-0246ͰఏҊ͞Εͨ͜ͱ
    ▸ protocol ElementaryFunctionsͱprotocol RealΛ࡞Δ
    ▸ ॳ౳ؔ਺ηοτ \subset ࣮ؔ਺ηοτ
    ▸ staticؔ਺ͷ͔ͨͪͰܭࢉͰ͖ΔΑ͏ʹ͢Δprotocol
    ▸ ͜ΕͰࠓޙ΋ͬͱ֦ு͍͚ͯ͠Δͧʂ
    ▸ ෳૉ਺ͱ͔SIMDͱ͔
    ▸ IEEE 754ͷ࣮૷͓͢͢Ίؔ਺Λίϯϓ͢Δͱ͔

    View Slide

  10. SE-0246: GENERIC MATH(S) FUNCTIONS
    ৽چൺֱ - SINؔ਺Λ࢖͏
    // 今のSwift

    let value: CGFloat = 0

    sin(value)

    // SE-0246

    // extension CGFloat: ElementaryFunction

    CGFloat.sin(0)

    View Slide

  11. SE-0246: GENERIC MATH(S) FUNCTIONS
    ৽چൺֱ - SIGMOIDؔ਺Λఆٛ͢Δ
    // エラー。全浮動小数点数型を一括で扱えない

    func sigmoid(_ x: T) -> T {

    return 1/(1 + exp(-x))

    }

    // SE-0246ならElementaryFunctionsと一言書けばよくなる

    func sigmoid(_ x: T) -> T {

    return 1/(1 + T.exp(-x))

    }
    σ(x) =
    1
    1 + e−x
    ※γάϞΠυؔ਺

    View Slide

  12. View Slide

  13. View Slide

  14. WITH MODIFICATION

    ͬͯͳʹ

    View Slide

  15. SE-0246: GENERIC MATH(S) FUNCTIONS
    ACCEPTED **WITH MODIFICATIONS**
    ▸ Կ͔ٞ࿦Λͨ͠ܗ੻͕͋Δʂ

    View Slide

  16. SE-0246: GENERIC MATH(S) FUNCTIONS
    ACCEPT͞ΕΔ·Ͱʹมߋ͞Εͨ͜ͱ
    ▸ Ұ෦ͷؔ਺͕ॳ౳ؔ਺͔Β࣮ؔ਺ߦ͖ʹ͞Εͨ
    ▸ atan2͸ඇ࣮਺ΛҾ਺ʹऔΒͳ͍
    ▸ erf, erfc, gamma, logGamma͸Ұ෦؀ڥͰ࢖͑ͳ͍͠ɺਖ਼֬ʹ͸

    ॳ౳ؔ਺͡Όͳ͍
    ▸ ࣮ؔ਺ʹ௥Ճ: hypot
    ▸ ໊લۭؒͷมߋ
    ▸ ؔ਺Λ“Math”Լʹ·ͱΊΑ͏ͱࢥ͚ͬͯͨͲ΍Ίͨ

    View Slide

  17. Ϟδϡʔϧ
    Ͳ͏͢Δ໰୊

    View Slide

  18. SE-0246: GENERIC MATH(S) FUNCTIONS
    ϞδϡʔϧͲ͏͢Δ໰୊
    ▸ MathϞδϡʔϧͰimport໰୊Λղܾ͢Δͭ΋Γ…ͩͬͨ
    ▸ αʔυύʔςΟϥΠϒϥϦͰ͢ͰʹMathͷ໊Λ࢖͍ͬͯΔ
    ▸ SwiftϞδϡʔϧʹ͢ͰʹmathͬΆ͍ؔ਺͕͋Δ

    ͱΓ͋͑ͣࠓճ͸Swiftʹ௚઀ಥͬࠐΉ͚ͩʹ͠Α͏ͥʂ

    View Slide

  19. LOGؔ਺ͷ໊લ

    Ͳ͏͢Δ໰୊

    View Slide

  20. SE-0246: GENERIC MATH(S) FUNCTIONS
    ֤ݴޠͷର਺ؔ਺ͷ໊લ
    ࣗવର਺ ৗ༻ର਺ ΧελϜ
    Swift log log10 N/A
    log log10 N/A
    java.lang.Math log log10 N/A
    kotlin.math ln log10 log

    View Slide

  21. SE-0246: GENERIC MATH(S) FUNCTIONS
    ͰɺͲ͏ͯ͠SWIFT͸LNͰ͸ͳ͘LOGʹͨ͠ͷ
    ▸ C++͕ͦ͏͔ͩΒ
    ▸ lnͩͱ໊લ͕୹͗͢Δ͔Β
    ▸ inͱฆΒΘ͍͔͠Β
    { x in ln(x) }

    { x in log(x) }

    View Slide

  22. ·ͱΊ

    View Slide

  23. SE-0246: GENERIC MATH(S) FUNCTIONS
    SE-0246͸͜Μͳײ͡ʂ
    ▸ ͍Ζ͍Ζͳܭࢉ͕Ͱ͖ΔΑ͏ʹͳΔΑ
    ▸ ࠓޙ͞ΒʹͰ͖Δ͜ͱ͕૿͑ͦ͏ͩΑ
    ▸ ࣮ͬͦ͘͞૷͚ͨ͠Ͳޓ׵ੑςετʁͰࣦഊ͔ͨ͠Βෆे෼

    View Slide

  24. SE-0246: GENERIC MATH(S) FUNCTIONS
    ࢀߟจݙͱ͔ͦ͏͍͏ͷ
    ▸ swift-evolution/0246-mathable.md at master · apple/swift-evolution
    ▸ Math functions for Swift by stephentyrone · Pull Request #23140 · apple/swift
    ▸ Revert SE-0246 by stephentyrone · Pull Request #23800 · apple/swift
    ▸ SE-0246: Generic Math(s) Functions - Proposal Reviews - Swift Forums
    ▸ [Accepted with Modifications] SE-0246: Generic Math Functions -
    Announcements - Swift Forums
    ▸ Generic “math functions” - Pitches - Swift Forums
    ▸ apple/swift-source-compat-suite: The infrastructure and project index
    comprising the Swift source compatibility suite.

    View Slide

  25. SE-0246: GENERIC MATH(S) FUNCTIONS
    ࢀߟจݙͱ͔ͦ͏͍͏ͷ
    ▸ 754-2008 - IEEE Standard for Floating-Point Arithmetic - IEEE Standard
    ▸ (math.h) - C++ Reference
    ▸ Math (Java SE 12 & JDK 12 )
    ▸ kotlin.math - Kotlin Programming Language

    View Slide