PHP: gmp_sign - Manual (original) (raw)

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

gmp_sign — Sign of number

Description

Parameters

num

Either a GMP object, or a numeric string provided that it is possible to convert the latter to anint.

Return Values

Returns 1 if num is positive, -1 if num is negative, and 0 if num is zero.

Examples

Example #1 gmp_sign() example

<?php // positive echo gmp_sign("500") . "\n";// negative echo gmp_sign("-500") . "\n";// zero echo gmp_sign("0") . "\n"; ?>

The above example will output:

Found A Problem?

thomas at zilliox dot me

14 years ago

Hi !

If you don't have the GMP extension, the sign function is really simple to code.
Here an example of implementation :

<?php
function sign( $number ) {
    return ( <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>u</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mo>&gt;</mo><mn>0</mn><mo stretchy="false">)</mo><mo stretchy="false">?</mo><mn>1</mn><mo>:</mo><mo stretchy="false">(</mo><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">number &gt; 0 ) ? 1 : ( ( </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7335em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">n</span><span class="mord mathnormal">u</span><span class="mord mathnormal">mb</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">0</span><span class="mclose">)?</span><span class="mord">1</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">((</span></span></span></span>number < 0 ) ? -1 : 0 );
}

echo sign( 500 ); // Return 1
echo sign( -500 ); // Return -1
echo sign( 0 ); // Return 0
?>

Thomas.

Andrew Martin

8 years ago

Using a spaceship in PHP7:

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">sign = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>number <=> 0;
?>