LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2008, 02:19 PM   #1
valembe
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Rep: Reputation: 0
Correction on float and int


Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	float d = 0.00;
	int x = 3, y = 9;
	
	d = x/y;
	
	cout<<d<<endl;
	return 0;
}
If i want a float result is the right way to do it??
 
Old 10-30-2008, 02:43 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by valembe View Post
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	float d = 0.00;
	int x = 3, y = 9;
	
	d = x/y;
	
	cout<<d<<endl;
	return 0;
}
If i want a float result is the right way to do it??
No, x or y would need to be a float or cast to a float in order for d to have the actual floating point answer.

Search on google for 'implicit type conversion'
 
Old 10-30-2008, 03:03 PM   #3
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
The compiler first looks at "x/y", and performs integer division because neither x nor y are floats. Then after it gets that value, it sees the lvalue is a float, so it converts the result to a float.
 
Old 10-30-2008, 08:09 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Adding to what others have said there is a floating point cast happening here.
Code:
float d = 0.00;
This is a double cast to a float, although initialisation is not required here it would be
Code:
float d = 0.f;
Just for completeness
Code:
#include <iostream>
int main()
{
	int x = 3, y = 9;
	float d( static_cast<float>(x)/y );
	std::cout<<d<<std::endl;
}

Last edited by dmail; 10-31-2008 at 05:59 AM. Reason: changed reinterpret_cast to static_cast
 
Old 10-30-2008, 09:04 PM   #5
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
why not just (float)x/y?
 
Old 10-31-2008, 05:59 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
"why not just (float)x/y?"
Because that is a C cast and the code is C++. It was late when I posted that and it really should be static_cast I will fix the post.
 
Old 11-01-2008, 12:02 PM   #7
valembe
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dmail View Post
"why not just (float)x/y?"
Because that is a C cast and the code is C++. It was late when I posted that and it really should be static_cast I will fix the post.
Thanx guys!! (float)x/(float)y worked..
 
Old 11-01-2008, 12:38 PM   #8
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by valembe View Post
Thanx guys!! (float)x/(float)y worked..
You really only need to do it on one of them. As long as one of them is a float, it will do floating point division.
 
Old 11-01-2008, 12:47 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by CRC123 View Post
You really only need to do it on one of them. As long as one of them is a float, it will do floating point division.
A better practice is to explicitly convert all the types involved.
 
Old 11-01-2008, 03:22 PM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
A better practice is to explicitly convert all the types involved.
Can you please explain why you think this is "better practice" please?
 
Old 11-01-2008, 03:29 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dmail View Post
Can you please explain why you think this is "better practice" please?
Because you do not have to remember what a compiler does for you and what it doesn't.

It's especially important if you deal with more than one language, and type conversion rules are different.
 
Old 11-02-2008, 05:11 AM   #12
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
Instead of doing a typecasting operation [ (float) x/ (float) y ] - why don't you define the variables itself as float [ float x = 3.00, y = 9.00; ]

As per me - that would be a better programming practice per se!


Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 04:50 AM.
 
Old 11-02-2008, 05:19 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by nishamathew1980 View Post
Instead of doing a typecasting operation [ (float) x/ (float) y ] - why don't you define the variables itself as float [ float x = 3.00, y = 9.00; ]

As per me - that would be a better programming practice per se!
Strictly saying, you should write

float x = 3.00f

not

float x = 3.00

- the latter means "3.00" is considered to be double.

Better yet, this is more or less what I do:

Code:
#define MY_FLOAT_TYPE float
// #define MY_FLOAT_TYPE double
...
MY_FLOAT_TYPE x = (MY_FLOAT_TYPE)3;
MY_FLOAT_TYPE y = (MY_FLOAT_TYPE)4.1;
, etc.

The points are:
  1. define a type macro (or a typdef);
  2. always explicitly declare with the above;
  3. always explicitly cast to the above.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid conversion from 'int' to 'unsigned int*' tigerhp Programming 2 03-02-2008 04:21 PM
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 08:30 AM
Converting int to float? pirate_pete Programming 5 09-15-2005 02:00 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration