Author Topic: Checking for a whole number.  (Read 6956 times)

0 Members and 1 Guest are viewing this topic.

Offline tjmbcTopic starter

  • Irregular
    • View Profile
Checking for a whole number.
« on: May 22, 2008, 01:33:53 PM »

I need to check to see if this is a whole number...
 
   $num_in / 5

Offline GingerRobot

  • Guru
  • Fanatic
  • *
  • Gender: Male
  • Call me Ben
    • View Profile
Re: Checking for a whole number.
« Reply #1 on: May 22, 2008, 01:47:36 PM »
Use the modulus operator:


if($num_in == 0){
    echo 
'Multiple of 5';
}else{
    echo 
'Not a multiple of 5';
}

Offline btherl

  • Guru
  • Addict
  • *
  • Matt is the best!
    • View Profile
Re: Checking for a whole number.
« Reply #2 on: May 22, 2008, 08:09:47 PM »
The input will need to be an integer for that to work .. as long as it is, it should be fine.
« Last Edit: May 22, 2008, 08:11:21 PM by btherl »

Offline deadonarrival

  • Devotee
    • View Profile
    • T Minus Ten - My personal site.
Re: Checking for a whole number.
« Reply #3 on: May 25, 2008, 06:50:50 PM »
Code: [Select]
$answer = $num_in / 5;
if(is_int($answer))
{
 #integer
}
else
{
 #not an integer
}
DO {
$this->time_warp("Jump to the left","Step to the right","Bend knees in time");
} WHILE (2>1)

Warning: The above post may not make a lot of sense, and the sentence structure will suck. I tend to ramble a lot. Sorry ;-)

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: Checking for a whole number.
« Reply #4 on: May 25, 2008, 07:33:55 PM »
@doa,

if $num_in = 10.00, then you get "not an integer" as the answer is a float (2.00)
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline deadonarrival

  • Devotee
    • View Profile
    • T Minus Ten - My personal site.
Re: Checking for a whole number.
« Reply #5 on: May 25, 2008, 07:39:06 PM »
Hmm, didn't know that one. Ignore my suggestion then :)
DO {
$this->time_warp("Jump to the left","Step to the right","Bend knees in time");
} WHILE (2>1)

Warning: The above post may not make a lot of sense, and the sentence structure will suck. I tend to ramble a lot. Sorry ;-)

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: Checking for a whole number.
« Reply #6 on: May 25, 2008, 07:44:44 PM »
I must admit, I had to test your code to make sure. You never know, it might catch on.
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline deadonarrival

  • Devotee
    • View Profile
    • T Minus Ten - My personal site.
Re: Checking for a whole number.
« Reply #7 on: May 26, 2008, 07:00:41 AM »
I would have thought it would make sense for is_int to check whether the number is a whole number, regardless of variable type. Or have a seperate function to check if the VALUE is an integer, rather than the TYPE.

Modulus never seems to work correctly for me, it's temperomental
DO {
$this->time_warp("Jump to the left","Step to the right","Bend knees in time");
} WHILE (2>1)

Warning: The above post may not make a lot of sense, and the sentence structure will suck. I tend to ramble a lot. Sorry ;-)

Offline The Little Guy

  • Fanatic
  • Gender: Male
  • A.K.A. TLG
    • View Profile
    • PHPSnips
Re: Checking for a whole number.
« Reply #8 on: May 27, 2008, 02:04:25 AM »
$myNumber 23.23;
if(
is_float($myNumber))
     echo 
'this is a float number';
else
     echo 
'this is not a float number';
Quote
There are NO bugs. Anything you see that may be buggy, is actually A FEATURE!!

Forum pet peeve: "Hi all" or "Hello all" ;)
Use: "Hi everyone" or "Hello everyone" ;)

JPG to ASCII Converter | Advanced Image CAPTCHA | Simple User Login | Check If User Is Logged In | Simple Image CAPTCHA | User Registration Form | Add (th, st, nd, rd, th) to the end of a number | Feet, Miles, And Inches Converter | AJAX Quickie | Create thumb from uploading photo | Is Prime?

Offline btherl

  • Guru
  • Addict
  • *
  • Matt is the best!
    • View Profile
Re: Checking for a whole number.
« Reply #9 on: May 27, 2008, 02:51:17 AM »
DOA, it does make sense to have a function that checks for integerness even if the value is not an integer, but is_int() isn't that function :)

If you write such a function you will have to decide what to do about large floats, which typically aren't integers even if they should be integers, due to floating point innacuracy.

Offline deadonarrival

  • Devotee
    • View Profile
    • T Minus Ten - My personal site.
Re: Checking for a whole number.
« Reply #10 on: May 27, 2008, 08:21:33 AM »
Yarp, but that's a problem I can never solve myself :)

Anyway, modulus is the answer to the original question - and binary inaccuracy is something we can keep for another day's musing :)
DO {
$this->time_warp("Jump to the left","Step to the right","Bend knees in time");
} WHILE (2>1)

Warning: The above post may not make a lot of sense, and the sentence structure will suck. I tend to ramble a lot. Sorry ;-)

Offline DarkWater

  • Freak!
  • Gender: Male
    • View Profile
Re: Checking for a whole number.
« Reply #11 on: June 07, 2008, 06:56:29 PM »
Or without calling any functions, you could do:

$num = $firstnumber / 2;
if ($num == (int) $num) {
 //It's whole
}
else {
 //It's not
}
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.


Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: Checking for a whole number.
« Reply #12 on: June 08, 2008, 05:31:18 AM »
It still doesn't allow for floating point inaccuracies. You can't depend on equality tests with FP values.

eg your number may be stored as 5.000000001, in which case you need to test if the difference is within tolerable bounds

Code: [Select]
if (abs($num - (int)$num) < 0.000001)
« Last Edit: June 08, 2008, 05:40:23 AM by Barand »
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline hitman6003

  • Addict
    • View Profile
Re: Checking for a whole number.
« Reply #13 on: June 12, 2008, 09:51:15 PM »
Perhaps some examples will help?

Code: [Select]
<?php

$a
['int'] =  (int) 4;
$a['float_but_int'] = (float) 4.00;
$a['normal_float'] = (float) 4.1;
$a['small_float'] = (float) 4.0000000000001;
$a['negative_float'] = (float) -5.00004;

foreach (
$a as $name => $value) {
echo $name "'s value is: " $value "\n";

if (is_int($value)) {
echo $name " is an integer.\n";
}

if (is_float($value)) {
echo $name " is a float.\n";
}

if ($value floor($value) == 0) {
echo $name " was determined to be a whole number (integer).\n";
}

if ($value floor($value) != 0) {
echo $name " was determined not to be a whole number (float).\n";
}

echo str_repeat("-"25) . "\n";
}

By subtracting floor($value) from $value and seeing if there is a remainder, you can determine if it's a whole number.

Offline corbin

  • Guru
  • Freak!
  • *
  • Gender: Male
    • View Profile
Re: Checking for a whole number.
« Reply #14 on: June 13, 2008, 01:59:11 AM »
hitman6003, I'm not positive, but I think that you may still have floating point innaccuracies with that....
Why doesn't anyone ever say hi, hey, or whad up world?

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.