 # floating point arithmetic in a script

Hello,

I am writing a script to process some tags which are floating point numbers.
I need to set a new tag to the difference between these floats. I have tried to use \$sub,
but it appears that \$sub only works if the numbers are integers. Is there a way to
perform floating point arithmetic?

Thanks,

Chuck

Not out of the box, I would say.
But you can use home-brew arithmetics with \$div and \$mod for the remainder. and naturally \$add and \$sub to add integers.
Yet, I wonder if it would not be easier to export the data, process it with a real calculation program and then re-import the result.

I do not think, that you have seen any tag-field having really a floating point number.
You might have seen the textual representation of a floating point number.

Mp3tag supports only integer math.

But there is a way to work with 'floating point' number strings.
Make sure to format the decimal part of all number strings (on the right side of the decimal point), so that all decimal parts get the same length (add rightmost '0' characters).
Then remove the decimal point from the number strings.
Now you can do integer arithmetic.

At end you might insert the decimal point at the right place into the numberstring.

Example

Subtract Number2 from Number1 N1 <== '123.45' N2 <== '0.54321' N1 <== \$regexp(%N1%\$repeat('0',6),'^(\d+)\.(\d{6}).+\$','\$1\$2') N2 <== \$regexp(%N2%\$repeat('0',6),'^(\d+)\.(\d{6}).+\$','\$1\$2') N3 <== \$sub(%N1%,%N2%) N3 <== \$cutRight(%N3%,6)'.'\$right(%N3%,6) ... or ... N3 <== \$div(%N3%,1000000)'.'\$mod(%N3%,1000000) N3 = '122.906790' N3 <== \$trimRight(%N3%,'0') N3 = '122.90679'

... or ...

Example

Subtract Number2 from Number1 N1 <== '123.45' N2 <== '0.54321' N0 <== \$ifgreater(\$len(\$regexp(%N1%,'^\d+\.',)),\$len(\$regexp(%N2%,'^\d+\.',)),\$len(\$regexp(%N1%,'^\d+\.',)),\$len(\$regexp(%N2%,'^\d+\.',))) N1 <== \$regexp(%N1%\$repeat('0',%N0%),'^(\d+)\.(\d{'%N0%'}).+\$','\$1\$2') N2 <== \$regexp(%N2%\$repeat('0',%N0%),'^(\d+)\.(\d{'%N0%'}).+\$','\$1\$2') N3 <== \$sub(%N1%,%N2%) N3 <== \$cutRight(%N3%,%N0%)'.'\$right(%N3%,%N0%) ... or ... N3 <== \$div(%N3%,'1'\$repeat('0',%N0%))'.'\$mod(%N3%,'1'\$repeat('0',%N0%)) N3 = '122.90679' Begin Action Group Test_2014#20140503.SubtractNum2FromNum1

Action #1
Actiontype 5: Format value
Field ______: MY_CALC_FLT1
Formatstring: 123.45

Action #2
Actiontype 5: Format value
Field ______: MY_CALC_FLT2
Formatstring: 0.54321

Action #3
Actiontype 5: Format value
Field ______: MY_CALC_L1
Formatstring: \$len(\$regexp(%MY_CALC_FLT1%,'^\d+.',))

Action #4
Actiontype 5: Format value
Field ______: MY_CALC_L2
Formatstring: \$len(\$regexp(%MY_CALC_FLT2%,'^\d+.',))

Action #5
Actiontype 5: Format value
Field ______: MY_CALC_L3
Formatstring: \$ifgreater(%MY_CALC_L1%,%MY_CALC_L2%,%MY_CALC_L1%,%MY_CALC_L2%)

Action #6
Actiontype 5: Format value
Field ______: MY_CALC_INT1
Formatstring: \$regexp(%MY_CALC_FLT1%\$repeat('0',%MY_CALC_L3%),'^(\d+).(\d{'%MY_CALC_L3%'}).+\$','\$1\$2')

Action #7
Actiontype 5: Format value
Field ______: MY_CALC_INT2
Formatstring: \$regexp(%MY_CALC_FLT2%\$repeat('0',%MY_CALC_L3%),'^(\d+).(\d{'%MY_CALC_L3%'}).+\$','\$1\$2')

Action #8
Actiontype 5: Format value
Field ______: MY_CALC_INT3
Formatstring: \$sub(%MY_CALC_INT1%,%MY_CALC_INT2%)

Action #9
Actiontype 5: Format value
Field ______: MY_CALC_FLT3
Formatstring: \$cutRight(%MY_CALC_INT3%,%MY_CALC_L3%)'.'\$right(%MY_CALC_INT3%,%MY_CALC_L3%)

Action #10
Actiontype 5: Format value
Field ______: MY_CALC_FLT3
Formatstring: \$trimRight(%MY_CALC_FLT3%,'0')

Action #11
Actiontype 5: Format value
Field ______: MY_CALC_FLT3
Formatstring: \$trimRight(%MY_CALC_FLT3%,'.')

Action #12
Actiontype 5: Format value
Field ______: MY_CALC_FLT3
Formatstring: \$replace(%MY_CALC_FLT3%,'.-','-.')

Action #13
Actiontype 9: Remove fields
Fields to remove (semicolon separated): MY_CALC_FLT1;MY_CALC_FLT2;MY_CALC_INT1;MY_CALC_INT2;MY_CALC_INT3;MY_CALC_L1;MY_C
ALC_L2;MY_CALC_L3

End Action Group Test_2014#20140503.SubtractNum2FromNum1 (13 Actions)

DD.20140428.2130.CEST, DD.20140429.1414.CEST, DD.20140503.1200.CEST