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
数値を文字列に整形する際の落とし穴とその解決策(iOSDC2024 ルーキーズLT) / iO...
Search
glassfiber
August 23, 2024
Programming
0
470
数値を文字列に整形する際の落とし穴とその解決策(iOSDC2024 ルーキーズLT) / iOSDC Japan 2024 Formatting Floating-Point Numbers
数値を文字列に整形する際に陥った落とし穴について、具体例を交えて紹介します。
glassfiber
August 23, 2024
Tweet
Share
More Decks by glassfiber
See All by glassfiber
pixiv App Night 20240523 NumberFormatterのハマり事例
glassfiber
0
140
Other Decks in Programming
See All in Programming
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
Jakarta EE meets AI
ivargrimstad
0
130
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
イベント駆動で成長して委員会
happymana
1
320
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
310
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
Click-free releases & the making of a CLI app
oheyadam
2
110
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
Featured
See All Featured
Speed Design
sergeychernyshev
24
610
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Faster Mobile Websites
deanohume
305
30k
Ruby is Unlike a Banana
tanoku
97
11k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Docker and Python
trallard
40
3.1k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Scaling GitHub
holman
458
140k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Transcript
glass fi ber (ϐΫγϒגࣜձࣾʣ Λจࣈྻʹܗ͢Δࡍͷ མͱ݀͠ͱͦͷղܾࡦ iOSDC Japan 2024 2024/08/23
Track A ϧʔΩʔζLT
00 iOS։ൃʹ6ϒϥϯΫͷ͋Δ ΤϯδχΞ͕෮ؼͨ͠Β ͷܗͰͲϋϚΓͨ͠
༻Λύʔηϯτදࣔ͢Δ • ετϨʔδͷ༻Λύʔηϯτදࣔ • ཁ: ͋Μ·Γͯ͘ݟʹ͍͘ͷͰ͍͍ײ͡ͷܻͰग़ͯ͠ཉ͍͠ (খୈ2Ґ͘Β͍ʁ) 00 ←͜Μͳײ͡ͷͭ
༻Λύʔηϯτදࣔ͢Δ • খୈ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ҐͰ࢛ࣺޒೖ͢Δ
• ʹΑͬͯظ͢ΔදࣔʹͳΒͳ͍͜ͱ͕໌ • ͨͱ͕͑0.523ͷͱ͖ • খҎԼͷܻ͕ͱͯ͘ͳͬͯ͠·͏ 52.300000000000004% ʮͳΜ͔ද͕͓͔ࣔ͘͠ͳΔʯͱ͍͏ใࠂ͕ 00 let
usageRate = 0.523 let value = round(usageRate * 10000) / 10000 let percentValue = value * 100 print("\(percentValue)%")
ͳΜͰ͜͏ͳΔͷ͔ • ුಈখͷޡ͕ࠩӨڹ • 2ਐͰදݱͰ͖ͳ͍ޡؚ͕ࠩ·ΕΔ • ࢛ࣺޒೖͨͣ͠ͳͷʹɺ100ഒͨ͠Β·ͨԼҐͷܻ͕ݱΕΔ… • ͱΓ͋͑ͣɺύʔηϯτද͍ࣔͨ͠ͱ͖ʹ100ഒͯ͠ͳΜͱ͔͠Α͏ͱ ͢ΔͷΑ͘ͳͦ͞͏
00
ܻΛࢦఆͨ͠ΒΑ͍ͷͰ • String(format:)ͰখҎԼͷܻΛࢦఆͨ͠ΒͲ͏Ͱ͠ΐ͏ • ݁Ռɺܻ͕ݻఆͳͷͰɺඌʹ0͕ͭ͘ • ͜ΕͰ͍͍͕ɺ͏ͪΐͬͱ͍͍ײ͡ʹ͍ͨ͠ • ඌͷ0ͳ͍ͨ͘͠ 00
let usageRate = 0.523 let value = round(usageRate * 10000) / 10000 print("\(String(format: "%.2f", value * 100))%") 52.30% ↑খҎԼ2ܻ
ͷܗͱ͍͑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ܻ
খҎԼ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%
ͰɺܻΛΓସ͑ͯΈΔ 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%Ҏ্
ͰɺܻΛΓସ͑ͯΈΔ 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% มΘͬͯͳ͍…ʁ
ͰɺܻΛΓସ͑ͯΈΔ 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% มΘͬͯͳ͍…ʁ ←ˋͷͰൺֱ͢Δؒҧ͍ (Α͘Δ)
ͰɺܻΛΓସ͑ͯΈΔ 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%Ҏ্
ͰɺܻΛΓସ͑ͯΈΔ 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%Ҏ্
ද͍͍ࣔײ͚ͩ͡Ͳɺίʔυ͕ؾʹೖΒͳ͍ • switchͰ͚ΔͷͳΜ͔ΠϚΠν 00 formatter.maximumFractionDigits = switch value { case
..<0.01: 2 case ..<0.1: 1 default: 0 }
Ͳ͏͍ͨ͠ͷ͔ߟ͑ͯΈΔ • ༗ޮࣈ͕2ܻɺͱ͍͏͜ͱͰΑͦ͞͏…ʁ • NumberFomatterʹ༗ޮܻΛࢦఆ͢Δػೳ͕͋Δ (maximumSigni fi cantDigits) 00 →
→ → 12% 1.2% 0.12% 0.12345 0.012345 0.0012345 ͜ͷͷͱ͖ ͜͏͍ͨ͠
༗ޮࣈ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ܻʹઃఆ Αͦ͞͏…ʁ
খ͍͞ͷͱ͖ʹ͘ͳͬͯ͠·͏ͱࢦఠ͞ΕΔ 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%
খ͍͞ͷͱ͖ʹ͘ͳͬͯ͠·͏ • খ͍͞ͷͱ͖ʹ͔ͬ͠Γ༗ޮࣈ2ܻͰग़ΔͷͰ͘ͳΔ • খ෦ͷܻࢦఆ(maximumFractionDigits) จࣈྻͷ੍͕͞ݶ͞ ΕΔ͕ɺ༗ޮܻࢦఆ(maximumSigni fi cantDigits) ͦ͏Ͱͳ͍
• ͦΕͦ͏ 00 print(myFormat(0.00012345)) print(myFormat(0.00000012345)) 0.012% 0.000012%
খ͍ܻ͞Λ࢛ࣺޒೖͯ͠ղܾ • ࠷ॳʹ͍ͬͯͨɺখୈ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%
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Ґ·ͰͷදهʹͳΔ( ςετΛॻ͍ͯ҆৺͢Δ • Ͳ͏ͳͬͯ΄͍͔͠ΛͪΌ Μͱॻ͍ͯ֬ೝ͢Δ • ࣮ࡍͷετϨʔδ༰ྔͰද ࣔΛ֬ೝ͢Δͷඇݱ࣮త • ϋϚΔͳͲΛཏྻ͠ ͯɺظ͢ΔදࣔʹͳΔ͜ ͱΛ֬ೝ
·ͱΊ • ුಈখͷѻ͍͍͠ • Λύʔηϯτදهʹܗ͢Δࡍʹ100ഒͯ͠Ͳ͏ʹ͔͠Α͏ͱ͠ ͍͚ͯͳ͍ • NumberFormatterͳͲΛ͍ͬͯͩ͘͞ • ༗ޮܻͷࢦఆศར͕ͩɺจࣈྻͷ͞Λ੍ݶͯ͘͠Εͳ͍ͷͰҙ
͕ඞཁ • maximumFractionDigits ͱಉ͡Α͏ͳͷͱࢥͬͯ͠·͏ϫφ • ςετΛॻ͍ͯ҆৺͠Α͏ 00
ࣗݾհ glass fi ber ϐΫγϒגࣜձࣾ ৽نࣄۀ෦ iOSΤϯδχΞ • 20238݄ த్ೖࣾ
• Pastelaͱ͍͏͓ֆඳ͖ΞϓϦͷ։ൃʹैࣄ • લ৬ήʔϜ։ൃऀ • C++, C#Λओʹ༻ • 6લ·ͰiOS༻ήʔϜ࡞͍ͬͯͨ(iOS3ʙ7ͷ͜Ζ) • ϒϥϯΫΛຒΊΔͨΊमߦத 00
00 ͓·͚
ුಈখͷதΛݟΔ • ුಈখ: IEEE754 (Double : ഒਫ਼ɺFloat: ୯ਫ਼) • ුಈখͷܭࢉΛϏοτ୯ҐͰݟΕΔπʔϧ
• IEEE 754 Calculator (http://weitz.de/ieee/) 00
ුಈখͷதΛݟΔ • 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
None