473,326 Members | 2,182 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,326 software developers and data experts.

Divide 2 integers and get a double result?

I have:

int a = 1;
int b = 2;

double c = a/b;

Is it somehow possible to divide these two integers and get the result
as a double 0.5? Or do they both have to be declared as doubles?
Jan 7 '07 #1
6 72175
Johs said:
I have:

int a = 1;
int b = 2;

double c = a/b;

Is it somehow possible to divide these two integers and get the result
as a double 0.5? Or do they both have to be declared as doubles?
int a = 1;
int b = 2;

double c = a;

c /= b;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 7 '07 #2
Johs wrote:
I have:

int a = 1;
int b = 2;

double c = a/b;

Is it somehow possible to divide these two integers and get the result
as a double 0.5? Or do they both have to be declared as doubles?
double c = (double)a / b;

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 7 '07 #3
Johs wrote:
>
I have:

int a = 1;
int b = 2;

double c = a/b;

Is it somehow possible to divide these two integers and get the result
as a double 0.5? Or do they both have to be declared as doubles?
double c = (double)a / b;

--
pete
Jan 7 '07 #4
"Johs" <sd*@sdf.comwrote in message news:en**********@news.net.uni-c.dk...
>I have:

int a = 1;
int b = 2;

double c = a/b;

Is it somehow possible to divide these two integers and get the result as
a double 0.5? Or do they both have to be declared as doubles?
If you trust the compiler:

double c = (double)a / b;

If you don't trust the compiler and have paranoid personality disorder:

double c = (double)((double)((double)((double)((double)a / (double)b))));
/* "double" MUST occur 7 times. */

Seriously, the first example works because the compiler is required to
convert and/or promote operands according to certain rules before doing the
division:

<BEGIN>
First, if either operand has type long double , the other operand is
converted to long double.
Otherwise, if either operand has type double , the other operand is
converted to double.
Otherwise, if either operand has type float , the other operand is converted
to type float.
Otherwise, the integral promotions are first applied to both operands and
then the following rules are applied.
If either operand has type unsigned long int, the other operand is converted
to unsigned long int.
Otherwise, if one operand has type long int and the other has type unsigned
int, if a long int can represent all values of an unsigned int, the operand
of type unsigned int is converted to long int; if a long int cannot
represent all the values of an unsigned int, both operands are converted to
unssigned long int
Otherwise, if either operand has type long int, the other operand is
converted to long int.
Otherwise, if either operand has type unsigned int, the other operand is
converted to unsigned int.
Otherwise, both operands have type int.
<END>

That is why it isn't necessary to cast b.

The second example has the advantage that the word "double" occurs 7 times.
If you program in a team environment, you'll find that examples like this
can hang around in the code for years. It won't be changed because your
colleagues (a)are afraid of offending you, (b)know that the code is
superfluous but are afraid of breaking something, especially because the
comment seems to imply it is necessary, (c)aren't competent enough to
understand that the code is superfluous.

Dave.
Jan 7 '07 #5
Johs wrote:
I have:

int a = 1;
int b = 2;

double c = a/b;

Is it somehow possible to divide these two integers and get the result
as a double 0.5? Or do they both have to be declared as doubles?
#include <stdio.h>

int main(void)
{
int a = 1;
int b = 2;
double c;

printf("[output]\na = %d, b = %d\n", a, b);
c = a / b;
printf("c = a / b = %g\n", c);
c = (double) a / b;
printf("c = (double)a / b = %g\n", c);
c = a / (double) b;
printf("c = a / (double)b = %g\n", c);
return 0;
}
[output]
a = 1, b = 2
c = a / b = 0
c = (double)a / b = 0.5
c = a / (double)b = 0.5

have you considered actually looking at an elementary book on C?
Randomly coding what you think might be C is not the way to learn it.
Jan 7 '07 #6
pete wrote:
>
Johs wrote:

I have:

int a = 1;
int b = 2;

double c = a/b;

Is it somehow possible to divide these two integers
and get the result as a double 0.5?
Or do they both have to be declared as doubles?

double c = (double)a / b;
Also...

double c = (a + 0.0) / b;

--
pete
Jan 8 '07 #7

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

Similar topics

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...
2
by: Poewood | last post by:
I declared 2 integers 'numerator' and 'denominator' and a double 'result'. If Numerator = 5 and deniminator = 145 then why when I put result in a string do I get 0? Example for a label text...
6
by: Jon Vaughan | last post by:
Can someone tell me why the following statement gives me the result of 0 : Math.Ceiling( 1 / 12); Well actually the above is simplefied from : private const int LocationsPerPage = 12; ...
6
by: Eric Lilja | last post by:
Hi, I need to implement a function that should take a double and split it into two integers. The decimalpart may be 0 but it may not be greater than 0.99 and not less than 0.01. In other words,...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
11
by: redefined.horizons | last post by:
I'm still pretty new to Python. I'm writing a function that accepts thre integers as arguments. I need to divide the first integer by te second integer, and get a float as a result. I don't want...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
8
by: =?Utf-8?B?bWljaGFlbGd3ZWllcg==?= | last post by:
Hello! I was working on some code the other day, and I came across an odd discrepancy between the decimal and the double type. If I attempt to divide a decimal by zero, the framework throws an...
4
by: bsmath | last post by:
I tried the program below to divide integers by integers (a, n, h etc. below) that equal C, but it does not divide correctly. Someone told me that "float C=float();" should have an epsilon...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.