473,385 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get a double from the quotient of two int values?

int a = 3;
int b = 7;
double x, y;
x = a / b;
y = (1.0 * a) / b;
System.out.println("x = " + x);
System.out.println("y = " + y);

produces

x = 0.0
y = 0.42857142857142855
I understand *why* x is 0.0 (the / operator returns an int, which is then
implicitly cast to a double).

But I'd like to know if "(1.0 * a) / b" is the correct way to get the double
answer?

--
Thanks,
Adam

Aug 16 '05 #1
3 6562
If you explicitly cast either operand to a double you'll get the double
division operator and the other operand will be implicitly cast.

So:
x = (double) a / b;
or:
x = a / (double) b;

should do it but maybe the following shows exactly what you're doing?

x = (double) a / (double) b;

Obviously:

double a = 3;

would do the job too. It really depends what you're trying to achieve.

Regards,

John

"Adam Funk" <a2****@yahoo.com> wrote in message
news:dd***********@godfrey.mcc.ac.uk...
int a = 3;
int b = 7;
double x, y;
x = a / b;
y = (1.0 * a) / b;
System.out.println("x = " + x);
System.out.println("y = " + y);

produces

x = 0.0
y = 0.42857142857142855
I understand *why* x is 0.0 (the / operator returns an int, which is then
implicitly cast to a double).

But I'd like to know if "(1.0 * a) / b" is the correct way to get the
double
answer?

--
Thanks,
Adam

Aug 16 '05 #2

"Adam Funk" <a2****@yahoo.com> wrote in message
news:dd***********@godfrey.mcc.ac.uk...
int a = 3;
int b = 7;
double x, y;
x = a / b;
y = (1.0 * a) / b;
System.out.println("x = " + x);
System.out.println("y = " + y);

produces

x = 0.0
y = 0.42857142857142855
I understand *why* x is 0.0 (the / operator returns an int, which is then
implicitly cast to a double).

But I'd like to know if "(1.0 * a) / b" is the correct way to get the
double
answer?


Cast the integers to doubles.

y = (double)a / (double)b

- Oliver
Aug 16 '05 #3
Oliver Wong wrote:
Cast the integers to doubles.

y = (double)a / (double)b


I can't explain know why I didn't think of that.
Thanks!

Aug 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
14
by: cj | last post by:
VB2003. I need a large positive integer. Which is larger int64 or double? I see int64 also apparently is known as long and will hold -9,223,372,036,854,775,808 through...
25
by: Digital Puer | last post by:
I'm getting a very weird bit corruption in a double. I am on an Intel Red Hat Linux box. uname -a returns: Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006 i686 i686 i386...
0
by: xhunga | last post by:
Symbolic computation : the derivative of a quotient. Hello, This time I try to simulate the derivative of a quotient. The same function use the quotient rule if it is a quotient (f/g) and the...
2
by: Synapse | last post by:
hello guys.. i do really need some help here. i have to do the 4 basic mathematical operation by entering 2 numbers with decimal. example. 1st num: 12.50 2nd num: 6.5 the answer must be in...
18
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
11
by: xz | last post by:
In a program, if a double is within 0.0001 from an int, then I treat it as int. For example, 10.00005 will be treated as int, while 10.001 is not. I am looking for an algorithm to implement...
1
by: BigBaz | last post by:
If we have a graph G=(N,E), where N is the set of nodes, and E is the set of edges. If we partition N into k parts (partition 1, 2,3...k). And this partition is given by an array, with the node...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.