Maroonbells (talk | contribs) |
|||
Line 1: | Line 1: | ||
{{mirc title|/bigfloat Command}} | {{mirc title|/bigfloat Command}} | ||
− | The '''/bigfloat''' command either reports whether the script is in 'bigfloat mode', or can force the script in/out of bigfloat mode, where operation of several math identifiers, commands etc are affected. Most notably, the results from {{mIRC|$calc}} are no longer limited to numbers within doubles range, | + | The '''/bigfloat''' command either reports whether the script is in 'bigfloat mode', or can force the script in/out of bigfloat mode, where operation of several math identifiers, commands etc are affected. |
+ | Most notably, the results from {{mIRC|$calc}} are no longer limited to numbers within doubles range, fractions are no longer limited to 6 digits, and $base automatically detects if input is larger than doubles range's max. | ||
== Synopsis == | == Synopsis == | ||
/bigfloat [on|off] | /bigfloat [on|off] | ||
/bigfloat | /bigfloat | ||
− | * NOTE: alternatively, individual script commands are shifted into bigfloat mode as soon as they encounter a variable name ending with '.bf', either as the variable being set, or its presence within the command string. The 'on' and 'off' mode can be silenced like .bigfloat on | + | * NOTE: alternatively, individual script commands are shifted into bigfloat mode as soon as they encounter a %variable name ending with '.bf', either as the variable being set, or its presence within the command string. The 'on' and 'off' mode can be silenced like .bigfloat on |
== Switches == | == Switches == | ||
none | none | ||
Line 22: | Line 23: | ||
/if () | /if () | ||
/while () | /while () | ||
+ | When these compare the $v1 and $v2 terms, being in .bigfloat mode causes the comparison to not be in doubles mode. For example, since doubles mode only keeps the most significant 53 bits, if() and while() both think that 2^53 and 2^53+1 are the same number. However this shows that /bigfloat mode is able to see them as different, and the 'match bigfloat' message does not display: | ||
+ | //var -s %a.bf 2 ^ 53 , %b.bf %a.bf + 1 , %a %a.bf , %b %b.bf | if (%a == %b) echo -a match doubles | if (%a.bf == %b.bf) echo -a match bigfloat | ||
var/set math: | var/set math: | ||
+ | note how %a.bf causes accurate result beyond doubles range, while the %a.bx variable only has 53 bits accuracy - though using the /bigfloat ON command would have enabled both. | ||
//var -s %a.bf 2 ^ 64 , %a.bx 2 ^ 64 | //var -s %a.bf 2 ^ 64 , %a.bx 2 ^ 64 | ||
* Set %a.bf to 18446744073709551616 | * Set %a.bf to 18446744073709551616 | ||
Line 44: | Line 48: | ||
$isbit $biton $bitoff | $isbit $biton $bitoff | ||
− | The scope of bigfloat mode is that the called alias inherits the mode setting from the parent, but the parent is not affected by any changed setting in the child alias. For example, the next alias is called from the editbox... | + | $base may be the only function which autodetects whether the input is larger than 2^53 and automatically jumps into bigfloat results, and the next example does not need to use .bf variables nor be in BIGFLOAT ON mode to return results with more than 53 bits of accuracy: |
+ | |||
+ | //echo -a $base($base($sha1(abc),16,10),10,16) same as... | echo -a $upper($sha1(abc)) | ||
+ | |||
+ | The scope of bigfloat mode is that the called alias inherits the mode setting from the parent, but the parent is not affected by any changed setting in the child alias. And, enabling /bigfloat ON is only in effect for the duration of that script, so it's not a global setting that can be applied to all scripts without placing /BIGFLOAT ON at the beginning of each alias and in each ON EVENT handler. For example, the next alias is called from the editbox... | ||
alias bftestsub { var %mode $bigfloat | bigfloat $1 | return $qt(%mode -> $bigfloat) } | alias bftestsub { var %mode $bigfloat | bigfloat $1 | return $qt(%mode -> $bigfloat) } | ||
Line 63: | Line 71: | ||
$true "$true -> $false" $true | $true "$true -> $false" $true | ||
</source> | </source> | ||
+ | |||
+ | While it can be simpler to just slap /bigfloat ON at the front of a script and think that's the best solution, be aware of several things before you do. | ||
+ | One is that the above shows that setting /BIGFLOAT ON in the parent alias does not get inherited into any child aliases unless they also issue the same command. | ||
+ | Another is that for some usage this can needlessly trigger MAPM math routines which slow down your script to give it accuracy that's not needed, and can sometimes be counterproductive. For example, while your script can be tolerant of the 6 digit fraction from $calc(%a.bx / %b.bx), it may not appreciate the 30 digit fractions from $calc() when not limited by $round. | ||
+ | Another is that you may need to verify the actual results to see if the bigfloat output is appropriate for your script, such as how in normal doubles mode it knows that $log2(2) is 1, but in .bigfloat mode it thinks the answer is 0.999999999999999985890799396948 | ||
+ | |||
+ | While this is named 'bigfloat' it also allows improved accuracy for extremely large 'bigintegers', though calculations can sometimes be very slow when some parameters are huge. However the speed of scripts can be affected by how well the operations can be supported by the MAPM package that is geared toward long and precise fractions, but doesn't have an optimized method for some simple things like bitwise math operations. Also, some functions like $powmod are intended for usage in bigfloat mode, and does not provide accurate math when some parameters are as small as sqrt(2^53). | ||
+ | |||
+ | By careful use of %variable.bf mixed with bigfloat on/off, you can get benefits from both systems, by obtaining accurance for some commands that need it, while skipping the slowdown for commands that don't. | ||
== Compatibility == | == Compatibility == | ||
Line 85: | Line 102: | ||
* {{mIRC|$max}} | * {{mIRC|$max}} | ||
* {{mIRC|$sorttok}} | * {{mIRC|$sorttok}} | ||
+ | * {{mIRC|$pi}} | ||
* {{mIRC|$sqrt}} | * {{mIRC|$sqrt}} | ||
* {{mIRC|$cbrt}} | * {{mIRC|$cbrt}} | ||
Line 102: | Line 120: | ||
* {{mIRC|$modinv}} | * {{mIRC|$modinv}} | ||
* {{mIRC|$powmod}} | * {{mIRC|$powmod}} | ||
+ | * {{mIRC|$factorial}} | ||
+ | * {{mIRC|$fibonacci}} |
Revision as of 09:45, 23 February 2023
The /bigfloat command either reports whether the script is in 'bigfloat mode', or can force the script in/out of bigfloat mode, where operation of several math identifiers, commands etc are affected.
Most notably, the results from $calc are no longer limited to numbers within doubles range, fractions are no longer limited to 6 digits, and $base automatically detects if input is larger than doubles range's max.
Synopsis
/bigfloat [on|off] /bigfloat
- NOTE: alternatively, individual script commands are shifted into bigfloat mode as soon as they encounter a %variable name ending with '.bf', either as the variable being set, or its presence within the command string. The 'on' and 'off' mode can be silenced like .bigfloat on
Switches
none
Parameters
- [on] - Moves the script's calculations into bigfloat mode.
- [off] - Moves the script's calculations out of bigfloat mode.
Example
//bigfloat on | echo -a bigfloat mode is $bigfloat | bigfloat off | echo -a bigfloat mode is $bigfloat //bigfloat off | echo -a bigfloat mode is $bigfloat but $left(%null.bf,0) now bigfloat mode is $bigfloat * bigfloat mode is $false but now bigfloat mode is $true
Affected
* Commands: /if () /while () When these compare the $v1 and $v2 terms, being in .bigfloat mode causes the comparison to not be in doubles mode. For example, since doubles mode only keeps the most significant 53 bits, if() and while() both think that 2^53 and 2^53+1 are the same number. However this shows that /bigfloat mode is able to see them as different, and the 'match bigfloat' message does not display: //var -s %a.bf 2 ^ 53 , %b.bf %a.bf + 1 , %a %a.bf , %b %b.bf | if (%a == %b) echo -a match doubles | if (%a.bf == %b.bf) echo -a match bigfloat var/set math: note how %a.bf causes accurate result beyond doubles range, while the %a.bx variable only has 53 bits accuracy - though using the /bigfloat ON command would have enabled both. //var -s %a.bf 2 ^ 64 , %a.bx 2 ^ 64 * Set %a.bf to 18446744073709551616 * Set %a.bx to 18446744073709552000 /inc and /dec and /hinc and /hdec //var -s %a.bf $calc(2^53) , %a.bx %a.bf | inc -s %a.bf | inc -s %a.bx * Inc %a.bf to 9007199254740993 * Inc %a.bx to 9007199254740992 //set %a.bf $calc(2^53) | set %a.bx %a.bf | inc -cs %a.bf | inc -cs %a.bx | timer 1 5 unset % $+ a.bf % $+ a.bx * %a.bf continues incrementing past 2^53, while %a.bx remains constant because in non-bigfloat mode, $calc(2^53+1) = $calc(2^53) Affects identifiers: * Longer fractions returned from trig functions $cos $cosh $acos $sin $sinh $asin $tan $tanh $atan $atan2 $hypot and from $log $log2 $log10 $sqrt $cbrt $base $calc $pi $round * Numbers larger than 2^53 handled without loss of precision. Note that this includes internal subtotals as well as the returned value $calc $abs $int $ceil $floor $isnum $gcd $modinv $lcm $numbits $powmod $rand $min $max $sorttok $sqrt $cbrt $log $log2 $log10 $and $xor $or $not $isbit $biton $bitoff $base may be the only function which autodetects whether the input is larger than 2^53 and automatically jumps into bigfloat results, and the next example does not need to use .bf variables nor be in BIGFLOAT ON mode to return results with more than 53 bits of accuracy: //echo -a $base($base($sha1(abc),16,10),10,16) same as... | echo -a $upper($sha1(abc)) The scope of bigfloat mode is that the called alias inherits the mode setting from the parent, but the parent is not affected by any changed setting in the child alias. And, enabling /bigfloat ON is only in effect for the duration of that script, so it's not a global setting that can be applied to all scripts without placing /BIGFLOAT ON at the beginning of each alias and in each ON EVENT handler. For example, the next alias is called from the editbox... alias bftestsub { var %mode $bigfloat | bigfloat $1 | return $qt(%mode -> $bigfloat) } //bigfloat off | bftestsub on | echo -a $bigfloat * BigFloat is off * BigFloat is on $false //bigfloat off | echo -a $bigfloat $bftestsub(on) $bigfloat * BigFloat is off * BigFloat is on $false "$false -> $true" $false //bigfloat on | echo -a $bigfloat $bftestsub(off) $bigfloat * BigFloat is on * BigFloat is off $true "$true -> $false" $true
While it can be simpler to just slap /bigfloat ON at the front of a script and think that's the best solution, be aware of several things before you do. One is that the above shows that setting /BIGFLOAT ON in the parent alias does not get inherited into any child aliases unless they also issue the same command. Another is that for some usage this can needlessly trigger MAPM math routines which slow down your script to give it accuracy that's not needed, and can sometimes be counterproductive. For example, while your script can be tolerant of the 6 digit fraction from $calc(%a.bx / %b.bx), it may not appreciate the 30 digit fractions from $calc() when not limited by $round. Another is that you may need to verify the actual results to see if the bigfloat output is appropriate for your script, such as how in normal doubles mode it knows that $log2(2) is 1, but in .bigfloat mode it thinks the answer is 0.999999999999999985890799396948
While this is named 'bigfloat' it also allows improved accuracy for extremely large 'bigintegers', though calculations can sometimes be very slow when some parameters are huge. However the speed of scripts can be affected by how well the operations can be supported by the MAPM package that is geared toward long and precise fractions, but doesn't have an optimized method for some simple things like bitwise math operations. Also, some functions like $powmod are intended for usage in bigfloat mode, and does not provide accurate math when some parameters are as small as sqrt(2^53).
By careful use of %variable.bf mixed with bigfloat on/off, you can get benefits from both systems, by obtaining accurance for some commands that need it, while skipping the slowdown for commands that don't.
Compatibility
Added: mIRC v7.72
Added on: 27 Nov 2022
Note: Unless otherwise stated, this was the date of original functionality.
Further enhancements may have been made in later versions.