Slide 1

Slide 1 text

glass fi ber (ϐΫγϒגࣜձࣾʣ ਺஋Λจࣈྻʹ੔ܗ͢Δࡍͷ མͱ݀͠ͱͦͷղܾࡦ iOSDC Japan 2024 2024/08/23 Track A ϧʔΩʔζLT

Slide 2

Slide 2 text

00 iOS։ൃʹ6೥ϒϥϯΫͷ͋Δ ΤϯδχΞ͕෮ؼͨ͠Β ਺஋ͷ੔ܗͰͲϋϚΓͨ͠࿩

Slide 3

Slide 3 text

࢖༻཰Λύʔηϯτදࣔ͢Δ • ετϨʔδͷ࢖༻཰Λύʔηϯτදࣔ • ཁ๬: ͋Μ·Γ௕ͯ͘΋ݟʹ͍͘ͷͰ͍͍ײ͡ͷܻ਺Ͱग़ͯ͠ཉ͍͠ (খ਺ୈ2Ґ͘Β͍ʁ) 00 ←͜Μͳײ͡ͷ΍ͭ

Slide 4

Slide 4 text

࢖༻཰Λύʔηϯτදࣔ͢Δ • খ਺ୈ2Ґ·Ͱදࣔ͢ΔίʔυΛॻ͖·ͨ͠ • ҰݟΑͦ͞͏ʹݟ͑·͢ ͕ 00 let usageRate = 0.12345 let value = round(usageRate * 10000) / 10000 let percentValue = value * 100 print("\(percentValue)%") 12.35% ↑0.01%=0.0001ͳͷͰ ɹখ਺ୈ5ҐͰ࢛ࣺޒೖ͢Δ

Slide 5

Slide 5 text

• ஋ʹΑͬͯ͸ظ଴͢ΔදࣔʹͳΒͳ͍͜ͱ͕൑໌ • ͨͱ͑͹஋͕0.523ͷͱ͖ • খ਺఺ҎԼͷܻ਺͕ͱͯ΋௕͘ͳͬͯ͠·͏ 52.300000000000004% ʮͳΜ͔ද͕͓͔ࣔ͘͠ͳΔʯͱ͍͏ใࠂ͕ 00 let usageRate = 0.523 let value = round(usageRate * 10000) / 10000 let percentValue = value * 100 print("\(percentValue)%")

Slide 6

Slide 6 text

ͳΜͰ͜͏ͳΔͷ͔ • ුಈখ਺఺਺ͷޡ͕ࠩӨڹ • 2ਐ਺ͰදݱͰ͖ͳ͍਺͸ޡؚ͕ࠩ·ΕΔ • ࢛ࣺޒೖͨ͠͸ͣͳͷʹɺ100ഒͨ͠Β·ͨԼҐͷܻ͕ݱΕΔ… • ͱΓ͋͑ͣɺύʔηϯτද͍ࣔͨ͠ͱ͖ʹ100ഒͯ͠ͳΜͱ͔͠Α͏ͱ ͢Δͷ͸Α͘ͳͦ͞͏ 00

Slide 7

Slide 7 text

ܻ਺Λࢦఆͨ͠ΒΑ͍ͷͰ͸ • String(format:)Ͱখ਺఺ҎԼͷܻ਺Λࢦఆͨ͠ΒͲ͏Ͱ͠ΐ͏ • ݁Ռ͸ɺܻ਺͕ݻఆͳͷͰɺ຤ඌʹ0͕ͭ͘ • ͜ΕͰ΋͍͍͕ɺ΋͏ͪΐͬͱ͍͍ײ͡ʹ͍ͨ͠ • ຤ඌͷ0͸ͳ͍ͨ͘͠ 00 let usageRate = 0.523 let value = round(usageRate * 10000) / 10000 print("\(String(format: "%.2f", value * 100))%") 52.30% ↑খ਺఺ҎԼ2ܻ

Slide 8

Slide 8 text

਺஋ͷ੔ܗͱ͍͑͹NumberFormatter • numberStyle = .percent ͱ͢Ε͹100ഒͤͣͱ΋ύʔηϯτදهʹͳΔ • ͢͹Β͍͠ 00 let usageRate = 0.523 let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.maximumFractionDigits = 2 if let str = formatter.string(from: NSNumber(value: usageRate)) { print(str) } 52.3% ←খ਺఺ҎԼ࠷େ2ܻ

Slide 9

Slide 9 text

খ਺఺ҎԼ2ܻʹͰ͖ͨɺ͚Ͳ • ཁ๬͕͖ͨ • 12.34%ͱ͍͏ͷ͸௕͍ɺ੔਺Ͱे෼ • Ͱ΋0.12%͸͜ͷ··খ਺఺ҎԼ2ܻग़ͯཉ͍͠ • →஋ʹΑܻͬͯ਺Λ͍͍ײ͡ʹ੾Γସ͍͑ͨ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = 2 return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12.34% 1.23% 0.12%

Slide 10

Slide 10 text

Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<1: 2 case ..<10: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্

Slide 11

Slide 11 text

Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<1: 2 case ..<10: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্ 12.34% 1.23% 0.12% มΘͬͯͳ͍…ʁ

Slide 12

Slide 12 text

Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<1: 2 case ..<10: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12.34% 1.23% 0.12% มΘͬͯͳ͍…ʁ ←ˋͷ਺஋Ͱൺֱ͢Δؒҧ͍ (Α͘΍Δ)

Slide 13

Slide 13 text

Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<0.01: 2 case ..<0.1: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্

Slide 14

Slide 14 text

Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<0.01: 2 case ..<0.1: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12% 1.2% 0.12% ͍͍ײ͡ ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্

Slide 15

Slide 15 text

දࣔ͸͍͍ײ͚ͩ͡Ͳɺίʔυ͕ؾʹೖΒͳ͍ • switchͰ෼͚Δͷ͸ͳΜ͔ΠϚΠν 00 formatter.maximumFractionDigits = switch value { case ..<0.01: 2 case ..<0.1: 1 default: 0 }

Slide 16

Slide 16 text

Ͳ͏͍ͨ͠ͷ͔ߟ͑ͯΈΔ • ༗ޮ਺ࣈ͕2ܻɺͱ͍͏͜ͱͰΑͦ͞͏…ʁ • NumberFomatterʹ͸༗ޮܻ਺Λࢦఆ͢Δػೳ͕͋Δ (maximumSigni fi cantDigits) 00 → → → 12% 1.2% 0.12% 0.12345 0.012345 0.0012345 ͜ͷ஋ͷͱ͖ ͜͏͍ͨ͠

Slide 17

Slide 17 text

༗ޮ਺ࣈ2ܻͰ੔ܗ͢Δ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.usesSignificantDigits = true formatter.maximumSignificantDigits = 2 func myFormat(_ value: Double) -> String { return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12% 1.2% 0.12% ←༗ޮܻ਺2ܻʹઃఆ Αͦ͞͏…ʁ

Slide 18

Slide 18 text

খ͍͞஋ͷͱ͖ʹ௕͘ͳͬͯ͠·͏ͱࢦఠ͞ΕΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.usesSignificantDigits = true formatter.maximumSignificantDigits = 2 func myFormat(_ value: Double) -> String { return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) print(myFormat(0.00012345)) print(myFormat(0.00000012345)) 12% 1.2% 0.12% 0.012% 0.000012%

Slide 19

Slide 19 text

খ͍͞஋ͷͱ͖ʹ௕͘ͳͬͯ͠·͏ • খ͍͞஋ͷͱ͖ʹ΋͔ͬ͠Γ༗ޮ਺ࣈ2ܻͰग़ΔͷͰ௕͘ͳΔ • খ਺෦ͷܻ਺ࢦఆ(maximumFractionDigits) ͸จࣈྻͷ௕੍͕͞ݶ͞ ΕΔ͕ɺ༗ޮܻ਺ࢦఆ(maximumSigni fi cantDigits) ͸ͦ͏Ͱ͸ͳ͍ • ͦΕ͸ͦ͏ 00 print(myFormat(0.00012345)) print(myFormat(0.00000012345)) 0.012% 0.000012%

Slide 20

Slide 20 text

খ͍ܻ͞Λ࢛ࣺޒೖͯ͠ղܾ • ࠷ॳʹ΍͍ͬͯͨɺখ਺ୈ5ҐͰͷ࢛ࣺޒೖΛ࠶ͼಋೖ͠·͢ • Α͏΍͍͍͘ײ͡ʹͰ͖·ͨ͠ 00 func myFormat(_ value: Double) -> String { let roundedValue = round(value * 10000) / 10000 return formatter.string(from: NSNumber(value: roundedValue)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) print(myFormat(0.00012345)) print(myFormat(0.00000012345)) ←খ਺ୈ5ҐͰ࢛ࣺޒೖ 12% 1.2% 0.12% 0.01% 0%

Slide 21

Slide 21 text

func test_࢖༻༰ྔ͕0ͷͱ͖ʹ0ύʔηϯτදهʹͳΔ() { let usage = 0 let limit = 1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0%") } func test_࢖༻༰ྔ͕0_01ύʔηϯτະຬͷͱ͖ʹ0ύʔηϯτදهʹͳΔ let usage = 12 * 1000 * 1000 let limit = 1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0%") } func test_࢖༻༰ྔ͕0_1ύʔηϯτະຬͷͱ͖ʹখ਺2Ґ·ͰͷදهʹͳΔ let usage = 123 * 1000 * 1000 let limit = 1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0.01%") } func test_࢖༻༰ྔ͕1ύʔηϯτະຬͷͱ͖ʹখ਺2Ґ·ͰͷදهʹͳΔ() let usage = 1234 * 1000 * 1000 let limit = 1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0.12%") } func test_࢖༻༰ྔ͕10ύʔηϯτະຬͷͱ͖ʹখ਺1Ґ·ͰͷදهʹͳΔ( ςετΛॻ͍ͯ҆৺͢Δ • Ͳ͏ͳͬͯ΄͍͔͠ΛͪΌ Μͱॻ͍ͯ֬ೝ͢Δ • ࣮ࡍͷετϨʔδ༰ྔͰද ࣔΛ֬ೝ͢Δͷ͸ඇݱ࣮త • ϋϚΔ਺஋ͳͲΛཏྻ͠ ͯɺظ଴͢ΔදࣔʹͳΔ͜ ͱΛ֬ೝ

Slide 22

Slide 22 text

·ͱΊ • ුಈখ਺఺਺ͷѻ͍͸೉͍͠ • ਺஋Λύʔηϯτදهʹ੔ܗ͢Δࡍʹ100ഒͯ͠Ͳ͏ʹ͔͠Α͏ͱ͠ ͯ͸͍͚ͳ͍ • NumberFormatterͳͲΛ࢖͍ͬͯͩ͘͞ • ༗ޮܻ਺ͷࢦఆ͸ศར͕ͩɺจࣈྻͷ௕͞Λ੍ݶͯ͘͠Εͳ͍ͷͰ஫ҙ ͕ඞཁ • maximumFractionDigits ͱಉ͡Α͏ͳ΋ͷͱࢥͬͯ͠·͏ϫφ • ςετΛॻ͍ͯ҆৺͠Α͏ 00

Slide 23

Slide 23 text

ࣗݾ঺հ glass fi ber ϐΫγϒגࣜձࣾ ৽نࣄۀ෦ iOSΤϯδχΞ • 2023೥8݄ த్ೖࣾ • Pastelaͱ͍͏͓ֆඳ͖ΞϓϦͷ։ൃʹैࣄ • લ৬͸ήʔϜ։ൃऀ • C++, C#Λओʹ࢖༻ • 6೥લ·Ͱ͸iOS༻ήʔϜ΋࡞͍ͬͯͨ(iOS3ʙ7ͷ͜Ζ) • ϒϥϯΫΛຒΊΔͨΊमߦத 00

Slide 24

Slide 24 text

00 ͓·͚

Slide 25

Slide 25 text

ුಈখ਺఺਺ͷத਎ΛݟΔ • ුಈখ਺఺਺: IEEE754 (Double : ഒਫ਼౓ɺFloat: ୯ਫ਼౓) • ුಈখ਺఺਺ͷܭࢉΛϏοτ୯ҐͰݟΕΔπʔϧ • IEEE 754 Calculator (http://weitz.de/ieee/) 00

Slide 26

Slide 26 text

ුಈখ਺఺਺ͷத਎ΛݟΔ • SwiftͰ͸ .bitPatternͰුಈখ਺఺਺ͷදݱΛ ֬ೝͰ͖Δ • .exponentBitPatternͱ.signi fi candBitPatternͰࢦ਺෦ͱԾ਺෦Λݸผ ʹݟΔ͜ͱ΋Մೳ 00 print("\(String(0.523.bitPattern, radix: 2))") print("\(String(0.523.exponentBitPattern, radix: 2))") print("\(String(0.523.significandBitPattern, radix: 2))") 11111111100000101111000110101001111110111110011101101100100011 1111111110 101111000110101001111110111110011101101100100011

Slide 27

Slide 27 text

No content