Problem with dividing two integers
#1

Hi guys,
I have problem with dividing two integers. I don't remember I had that problem before :S
So, in main() I added simple printf for check

Код:
main()
{
	print(">>> SUSTAV POKRENUT");
	print("- Inacica skripte: "_HRP_SRVSETT_VR_L".");
	print("- Zadnje azuriranje: "_HRP_SRVSETT_LSTUP" godine.");
	print("- Kodno ime skripte: "_HRP_SRVSETT_CNAME_VR".");
	print("- Kodno ime azuriranja: "_HRP_SRVSETT_CNAME_UPD".");
	print("- Zahvale: "_HRP_SVRSETT_CRDS".");
	print("- Web adresa zajednice: "_HRP_SVRSETT_WEB".");
	print("- Klijent inacica: "_HRP_SVRSETT_CLN_VR".");
	print("- Server posluzitelj: "_HRP_SVRSETT_SRVHST".");
	print("- Web posluzitelj: "_HRP_SVRSETT_WEBHST".");
	print("- e-Mail zajednice: "_HRP_SVRSETT_MAIL".");
	print(" ");

	printf("%.2f", 5 / 2);
}
After I've compiled script and started the server, result was '0.00'. I'm not sure why I have this problem, I thought it was because I'm using rBits but it wasn't.
Also, if I divide two float numbers(eg. 5.5 / 2. I got right result. Same thing with dividing float with int and int with float values.
Problem is dividng with two int values but I don't Know where's a problem. I tried without includes I use and I got same problem.

Код:
// GLAVNI INCLUDEOVI
#include 															<a_samp>
#include 															<crashdetect>

	
// ****** ZBIRKA
#include															<YSI\y_timers>
#include 															<YSI\y_va>
#include 															<YSI\y_hooks>

// OSTALI INCLUDOVI
#include 															<a_mysql>
#define 															USE_OPTIMAL_MODE
#include 															<new_funcs>
#include 															<rBits>
#include 															<easyDialog>
#include 															<randomFloat>
#include 															<streamer>
#include 															<sscanf2> 
#include 															<LIFE-CMD>
#include                                            				<e-Mail>
#include 															<CTime>
I've tried with

Код:
	new Float:_var = (5 / 2);
	printf("%.2f", _var);
And it gave me 2.00(like I wrote 5 % 2)
Thank You guys very much.
Reply
#2

If you use arithmetic with different data types the "weaker" data type will be converted

Now if you divide a Float with an Integer, the Integer will be converted to a Float, resulting in a Float
But if you do a simple Integer division you get as result an integer
PHP код:
printf("%f"5.0 2.0); // float / float = float => 2.5
printf("%f"5.0 2); // float / int = float / float(int) = float => 2.5
printf("%f"2.0); // int / float = float(int) / float = float => 2.5
printf("%d"2); // int / int = int => 2 (the .5 gets chopped away) 
Reply
#3

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
If you use arithmetic with different data types the "weaker" data type will be converted

Now if you divide a Float with an Integer, the Integer will be converted to a Float, resulting in a Float
But if you do a simple Integer division you get as result an integer
PHP код:
printf("%f"5.0 2.0); // float / float = float => 2.5
printf("%f"5.0 2); // float / int = float / float(int) = float => 2.5
printf("%f"2.0); // int / float = float(int) / float = float => 2.5
printf("%d"2); // int / int = int => 2 (the .5 gets chopped away) 
Wait, but I got 0.00 instead 2. And why I can't 5 print like 5.00? I don't remember I can't do like that before.

It Works with this code

Код:
printf("%.2f", float(5) / float(2));
Reply
#4

Quote:
Originally Posted by vannesenn
Посмотреть сообщение
Wait, but I got 0.00 instead 2. And why I can't 5 print like 5.00? I don't remember I can't do like that before.

It Works with this code

Код:
printf("%.2f", float(5) / float(2));
Did you look at my code, if you do Int / Int you get as result an Int, thus you need to use %d / %i not %f
Also what you were looking for would be the first line "printf("%f", 5.0 / 2.0);", if you add a .0 to the number you declare them as float

PHP код:
printf("%f"5); // %f - float but the given parameter "5" is an integer => unknown result
printf("%f"5.0); // correct
printf("%d"5.0); // wrong
printf("%d"5); // correct 
Reply
#5

Well, okey. Very weird problem... Are these rules exist from begin or they are something new?
Reply
#6

It is like that in every language that has different data types and it's nothing new. Integer divided by integer remains integer. Any would-be fractional part is discarded.
Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
It is like that in every language that has different data types and it's nothing new. Integer divided by integer remains integer. Any would-be fractional part is discarded.
I Know that only for C++ but not for PAWN because I thought it supports int / int = float
Whatever, thanks guys for help.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)