분수 패키지는 Dart 및 Flutter 애플리케이션에서 분수와 혼합 분수를 다루는 도구를 제공합니다. 분수에 대한 정확한 산술 연산(덧셈, 뺄셈, 곱셈, 나눗셈)과 분수와 소수 간의 변환을 지원합니다. 패키지는 자동으로 기약분수로 줄이며, 직관적인 입력과 출력을 위한 혼합 분수도 지원합니다. 교육용 앱, 수학 도구 또는 부동소수점 오차 없이 정확한 분수 계산이 필요한 모든 애플리케이션에 적합합니다.
사용 사례
수학 교육 앱
분수 기반 계산기
레시피 스케일링 도구
과학 계산
금융 앱에서의 데이터 정밀도
주요 기능
분수 및 혼합 분수 지원
결과의 자동 단순화
산술 연산 (덧셈, 뺄셈, 곱셈, 나눗셈)
분수와 소수 간의 변환
불변 분수 객체
적합한 대상
수학 중심 앱을 개발하는 개발자
교육 소프트웨어 제작자
정확한 분수 계산이 필요한 앱
고려 사항
매우 큰 숫자에 최적화되지 않음
기본적인 산술 연산에 국한됨
표시 목적을 위한 내장 형식 지정 없음
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
A package that helps you work with fractions and mixed fractions.
fromDouble: creates a Fraction from a double value. Note that irrational numbers cannot be converted into fractions by definition. The constructor has the precision parameter which decides how precise the representation has to be.
For example, the constant pi cannot be represented as a fraction because it's an irrational number. The constructor considers only precison decimal digits to create the fraction.
Thanks to extension methods, you can create a Fraction object "on the fly" by calling the toFraction() method on a number or a string.
The Fraction type is immutable, so methods that require changing the object's internal state return a new instance. For example, the reduce() method reduces the fraction to the lowest terms and returns a new object:
You can easily sum, subtract, multiply and divide fractions using arithmetic operators:
final f1 = Fraction(5, 7); // 5/7final f2 = Fraction(1, 5); // 1/5final sum = f1 + f2; // 32/35final sub = f1 - f2; // 18/35final mul = f1 * f2; // 1/7final div = f1 / f2; // 25/7
The Fraction type has a wide API with the most common operations you'd expect to make on a fraction:
In the last example, any other value different from 0 and 1 throws a FractionException exception. Two fractions are equal if their "cross product" is equal. For example 1/2 and 3/6 are said to be equivalent because 1*6 = 3*2 (and in fact 3/6 is the same as 1/2).
Working with mixed fractions
A mixed fraction is made up of a whole part and a proper fraction (a fraction in which numerator <= denominator). Building a MixedFraction object is very easy:
You can create new MixedFraction objects using extension methods:
final mixed = "1 1/2".toMixedFraction();
The MixedFraction type is immutable, as it happens with Fraction. As such, you're guaranteed that the internal object state will never change. Make sure to check the official documentation at pub.dev for a complete overview of the API.
Egyptian fractions
An Egyptian fraction is a finite sum of distinct fractions where the numerator is always 1, the denominator is a positive number, and all the denominators differ. For example:
5/8 = 1/2 + 1/8 (where "1/2 + 1/8" is the egyptian fraction)
In other words, Egyptian fractions are a sum of fractions in the form 1/x that represent a proper or an improper fraction. Here's how they can be computed:
final egyptianFraction1 = Fraction(5, 8).toEgyptianFraction();
print("$egyptianFraction1"); // prints "1/2 + 1/8"final egyptianFraction2 = MixedFraction(2, 4, 5).toEgyptianFraction();
print("$egyptianFraction2"); // prints "1 + 1 + 1/2 + 1/4 + 1/20"
The toEgyptianFraction() method returns a List<Fraction> object.
Notes
Note that Fraction and MixedFraction are subtypes of Rational, which can be used for parsing both of kinds of fractions with a single method call. For example:
// Returns a 'Fraction' objectRational.tryParse('1/5'); // 1/5// Returns a 'MixedFraction' objectRational.tryParse('2 4/7'); // 2 4/7// This is 'null' because the string doesn't represent a valid fraction or mixed fractionRational.tryParse(''); // null
Parsing integer values such as Rational.tryParse('3') always returns a Fraction type.