adaptive_number 패키지는 런타임 환경에 따라 자동으로 적응하는 숫자 유형을 제공합니다. JavaScript 플랫폼에서는 일관된 정밀도를 위해 64비트 부호 있는 고정 폭 정수를 사용하지만, 다른 모든 플랫폼에서는 Dart의 기본 int를 기본값으로 사용합니다. 이는 웹 및 네이티브 환경 간에 신뢰할 수 있는 동작을 보장하며, 특히 큰 정수를 다루거나 수치 연산에서 크로스플랫폼 일관성이 필요한 경우에 유용합니다.
사용 사례
크로스 플랫폼 정수 처리
대규모 웹 기반 애플리케이션
플랫폼 간 일관된 산술
정수를 포함하는 데이터 직렬화
Flutter 웹 앱에서의 수치 계산
주요 기능
플랫폼에 따라 적응하는 정수 크기
JS에서의 64비트 고정 폭 정수
비-JS 플랫폼에서의 네이티브 int 백업
런타임 간 일관된 동작
기존 코드와의 간단한 통합
적합한 대상
웹 및 네이티브 플랫폼을 대상으로 하는 개발자
정밀한 정수 연산이 필요한 앱
숫자 논리의 이식성이 필요한 프로젝트
JS 고유의 숫자 제한을 피하는 팀
공유 비즈니스 로직을 가진 Flutter 앱
고려 사항
정수 값으로만 제한됨
부동 소수점 숫자 지원 없음
비-JS 플랫폼에서의 성능 오버헤드
간단한 사용 사례에서는 필요하지 않을 수 있습니다
JS 대상에서 신중한 테스트가 필요합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
adaptive_number
A library providing a Number implementation that is dependent on the platform it is compiled for.
For JS runtimes, the implementation uses a 64-bit signed fixed-width integer, for all other platforms
the default Dart int data type.
Use cases of this library
The Number substitute is useful in situations where a value may overflow on JS runtimes since the available value range
is not the same compared to mobile or desktop platforms:
The overflow on JS environments can be circumvented by using a BigInt or the fixed-with implementation of this library:
https://pub.dev/packages/fixnum
The disadvantage of just replacing all usages of int with BigInt or fixnum/Int64 is that the performance on
mobile platforms may suffer significantly.
The adpative_number library takes a conditional import approach to ease this problem:
If it runs on a JS runtime, Number wraps a fixnum/Int64
For all other platforms Number wraps a default Dart int type.
Some historical background of this library can also be seen in: https://github.com/MixinNetwork/libsignal_protocol_dart/pull/43
Usage
A Number instance is created by wrapping it around a default int value:
import'package:adaptive_number/adaptive_number.dart';
voidmain() {
final number1 = Number(93432);
final number2 = Number(42435);
assert((number1 + number2) == Number(51777));
}
All other operations on Number behave the same way as with int.
Features and bugs
Please file feature requests and bugs at the issue tracker.