SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Help Archive

 
 
Thread Tools Display Modes
Old 30/07/2011, 02:35 PM   #1
pen_theGun
Big Clucker
 
pen_theGun's Avatar
 
Join Date: Jan 2008
Posts: 179
Reputation: 0
Default Divide operator doesnt work properly?

Do i something wrong or does divide operator work incorrect?! Here are some examples

pawn Code:
printf( "%f", 5.0/8 ); // OK => 0.625000
printf( "%f", 5/8.0 );  // OK => 0.625000
printf( "%f", 5/8 ); // Wrong? => 0.000000
printf( "%d", 5/8 ); // Wrong? => 0

printf( "%d", 10/5 ); // OK => 2
printf( "%f", 10/5 ); // Wrong? => 0.000000
printf( "%f", float(10/5) ); // OK => 2.000000

Isnt job of print to provide me an correct output, instead of making float(10/5) etc.?
__________________



pen_theGun is offline  
Old 30/07/2011, 02:50 PM   #2
jamesbond007
Gangsta
 
jamesbond007's Avatar
 
Join Date: Oct 2009
Location: Canadia
Posts: 788
Reputation: 50
Default Re: Divide operator doesnt work properly?

looks right u cant use whole numbers when using float

use instead :

pawn Code:
printf("%f", 10/5.0);
jamesbond007 is offline  
Old 30/07/2011, 03:58 PM   #3
cessil
High-roller
 
cessil's Avatar
 
Join Date: Apr 2009
Posts: 1,353
Reputation: 296
Default Re: Divide operator doesnt work properly?

use floatdiv(float1,float2)
cessil is offline  
Old 30/07/2011, 05:06 PM   #4
Double-O-Seven
Guest
 
Posts: n/a
Default Re: Divide operator doesnt work properly?

I can tell you what's wrong: Nothing.

pawn Code:
printf( "%f", 5.0/8 ); // OK => 0.625000
printf( "%f", 5/8.0 );  // OK => 0.625000
printf( "%f", 5/8 ); // Wrong? => 0.00000  NOT WRONG! 5 / 8 == 0 is an integer division. (See below)
printf( "%d", 5/8 ); // Wrong? => 0 NOT WRONG! That's an integer divison. 5 / 8 is not an integer and smaller than 1.

printf( "%d", 10/5 ); // OK => 2
printf( "%f", 10/5 ); // Wrong? => 0.000000 NOT WRONG! 10 / 5 == 2 is an integer division.
// It's binary form is 0b0000...010 (30 zeros, 1 one and 1 zero.). However, 0b000...010 as integer is not the same number as floating point number which is in this case probably a number very close to 0.
printf( "%f", float(10/5) ); // OK => 2.000000

The only problem here is that you did not understand what exactly happens.
 
 

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Register/login doesnt work properly, help is needed. Gemini Help Archive 3 11/02/2011 04:07 PM
Why won't this work properly? Scenario Help Archive 24 11/12/2010 11:54 PM
Bank menu doesnt work properly when many bots are on? aNdReSkKkK Help Archive 0 08/02/2010 05:40 AM
Open Gate Command doesnt Work Properly [GM]The_Don Help Archive 4 07/02/2010 02:17 PM


All times are GMT. The time now is 08:14 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.