Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2005, 5:53 PM   #1
aslate
Newbie
 
Join Date: Apr 2005
Posts: 1
Rep Power: 0 aslate is on a distinguished road
$n * 1000 and Mod

I've been using fmod to validate data entry in Terry's Turkeys. You need to check weights are entered in 5g increments. So i take the input, *1000 and take the modulus for 5.

This threw up an odd bug when i inputted 8.065kg. Perfectly valid, as you can see. But rejected by my code. So i did a test:

for ($n = 0; $n < 10; $n += 0.005)
{
	$largen = $n*1000;
	$mod = $largen % 5;
	if (fmod(($largen), 5) != 0 || $largen % 5 != 0) 
	{ 
		echo "<tr><td>$n</td><td>$largen</td><td>".fmod(($largen), 5)."</td><td>".$mod."</td></tr>";
	}

You'll find the output rather odd: Results

Why's it doing this? It's not just fmod() being weird as it's for floating points, but % too.
aslate is offline   Reply With Quote
Old Apr 27th, 2005, 6:00 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Hope you get this working, mate.

Oh, and could you put borders on that table? It's hard to read.

EDIT: Got it:
$n = round($n, 5);
__________________
Me :: You :: Them

Last edited by Ooble; Apr 27th, 2005 at 6:41 PM.
Ooble is offline   Reply With Quote
Old May 20th, 2005, 3:48 PM   #3
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4 mackenga is on a distinguished road
Yeah, the trouble is that although it's an integer, it's an integer stored in a floating point variable and you can't use modulo with floating point, even if it happens to contain an integer value.
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC