Square root: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
mNo edit summary
imported>PhilmASTErpLus
Added code sample--someone please look, I don't know whether integer division causes slicing.
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{
{{
#vardefine:name|square_root}}{{
#vardefine:name|square_root}}{{
#vardefine:return_type|int}}{{
#vardefine:return_type|float}}{{


FunctionPage|
FunctionPage|
name={{#var:name}}|
name={{#var:name}}|
function_category=Math and Numbers|


function1={{Function|
function1={{Function|
name={{#var:name}}|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
aggregate={{#var:aggregate}}|
return_type=float|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
return_also={{#var:return_also}}|
parameter1={{Param|float|number}}|
parameter1={{Param|float|number}}|
Line 18: Line 17:
function_description=Returns the square root of the specified {{pspan|number}}. Note that it is possible supply an int type number as the parameter {{pspan|number}}, in which case it will be transparently converted to a float; the value returned will still be of float type (even if the int value would be equal, such as specifying 4 as {{pspan|number}}).|
function_description=Returns the square root of the specified {{pspan|number}}. Note that it is possible supply an int type number as the parameter {{pspan|number}}, in which case it will be transparently converted to a float; the value returned will still be of float type (even if the int value would be equal, such as specifying 4 as {{pspan|number}}).|


needscode=yes|
code1={{CodeSample|
title=Code Sample|
description=The following code will convert raw [http://kol.coldfront.net/thekolwiki/index.php/Damage_Absorption Damage Absorption] to the actual percentage of damage absorbed in a floating-point number.|
code=<syntaxhighlight>
int my_DA = 550;
float actual_absorbed = square_root( min( max( 10 , my_DA ) , 1000 ).to_float() / 1000 ) - 0.1;
</syntaxhighlight>|
moreinfo=[[to_float]]() is used to prevent fractions from being sliced. You can use [[raw_damage_absorption]]() and [[damage_absorption_percent]]() to determine your current damage absorption.
}}
 


}}
}}
[[Category:Math and Numbers]]

Latest revision as of 10:40, 26 June 2010

Function Syntax

float square_root(float number )

  • number is the number to find the root of

Returns the square root of the specified number. Note that it is possible supply an int type number as the parameter number, in which case it will be transparently converted to a float; the value returned will still be of float type (even if the int value would be equal, such as specifying 4 as number).

Code Sample

The following code will convert raw Damage Absorption to the actual percentage of damage absorbed in a floating-point number.

int my_DA = 550;
float actual_absorbed = square_root( min( max( 10 , my_DA ) , 1000 ).to_float() / 1000 ) - 0.1;

to_float() is used to prevent fractions from being sliced. You can use raw_damage_absorption() and damage_absorption_percent() to determine your current damage absorption.