Skip to content

Commit e718936

Browse files
committedMay 30, 2024
Add arithmetic scaffolding + global add function
1 parent 0307fac commit e718936

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed
 

‎latex/fractional-math.sty

+46-1
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,54 @@
9494
}
9595

9696
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97-
% Math
97+
% Arithmetic
9898
%
9999

100+
\cs_new_protected:Nn \fracmath_prepare:N
101+
{
102+
\prop_put_if_new:Nnn #1 { integer } { 0 }
103+
\prop_put_if_new:Nnn #1 { numerator } { 0 }
104+
\prop_put_if_new:Nnn #1 { denominator } { 1 }
105+
}
106+
107+
\cs_new_protected:Nn \fracmath_garithmetic:NnN
108+
{
109+
\prop_if_exist:NF #1 {
110+
\prop_gclear_new:N #1
111+
}
112+
\fracmath_prepare:N #1
113+
\fracmath_prepare:N #3
114+
115+
\regex_match_case:nn {
116+
{ \A \+ \Z } { \fracmath_gadd:NN #1 #3 }
117+
} { #2 }
118+
}
119+
\cs_generate_variant:Nn \fracmath_garithmetic:NnN
120+
{
121+
cnN, cVN
122+
}
123+
124+
% Adds two a fractional numbers
125+
\cs_new_protected:Nn \fracmath_gadd:NN
126+
{
127+
\int_zero_new:N \l__fracmath_tmp_value_int
128+
\int_set:Nn \l__fracmath_tmp_value_int {
129+
\prop_item:Nn #1 { integer } + \prop_item:Nn #2 { integer }
130+
}
131+
\prop_gput:NnV #1 { integer } \l__fracmath_tmp_value_int
132+
133+
\int_set:Nn \l__fracmath_tmp_value_int {
134+
( \prop_item:Nn #1 { numerator } * \prop_item:Nn #2 { denominator } ) +
135+
( \prop_item:Nn #2 { numerator } * \prop_item:Nn #1 { denominator } )
136+
}
137+
\prop_gput:NnV #1 { numerator } \l__fracmath_tmp_value_int
138+
139+
\int_set:Nn \l__fracmath_tmp_value_int {
140+
\prop_item:Nn #1 { denominator } * \prop_item:Nn #2 { denominator }
141+
}
142+
\prop_gput:NnV #1 { denominator } \l__fracmath_tmp_value_int
143+
}
144+
100145
% Reduces a value, eg 1 6/4 => 2 1/2
101146
\cs_new_protected:Nn \fracmath_reduce:N
102147
{

0 commit comments

Comments
 (0)