Charles Davis cdavis@mymail.mines.edu wrote:
On 10/31/10 9:23 PM, James McKenzie wrote:
All:
I'm trying to convert one integer number using a float number factor. Here is the code:
int ry = 125; double ryf;
ryf = ry/1440;
The result is 0 and should be 0.06666666667 (or even more sixes.)
And why should it be? None of the values in the expression 'ry/1440' are of type 'double'. They're both 'int's, so the compiler sees it as integer division instead of FP division. The C language spec doesn't require the compiler to infer that you wanted FP division when you assign to a variable of FP type.
Ah, the wonders of 'c'. Thank you for the reminder that this is what happens during this division process. Been a long time since I've tried this.
What am I doing 'incorrectly'?
Now, if your assignment looked like this:
ryf = ry/1440.0;
then it would work correctly. Now the 1440 is of type 'double', so the compiler emits a floating-point division.
Got it. I tried to cast ry to double, but that did not work. Guess my version of gcc has a problem or I did it wrong.
Will try changing the division this evening and see what happens.
James McKenzie
--- On Mon, 1/11/10, James Mckenzie jjmckenzie51@earthlink.net wrote:
From: James Mckenzie jjmckenzie51@earthlink.net Subject: Re: Problems with Test To: "Charles Davis" cdavis@mymail.mines.edu Cc: "wine-devel@winehq.org" wine-devel@winehq.org Date: Monday, 1 November, 2010, 14:26 Charles Davis cdavis@mymail.mines.edu wrote:
On 10/31/10 9:23 PM, James McKenzie wrote:
All:
I'm trying to convert one integer number using a
float number factor.
Here is the code:
int ry = 125; double ryf;
ryf = ry/1440;
<snipped>
Got it. I tried to cast ry to double, but that did not work. Guess my version of gcc has a problem or I did it wrong.
<snipped>
- ryf = ((double) ry)/1440; - should work# - ryf = (double) ry/1440; - does not work, because it is equivalent to "(double) (ry/1440) " (still integer division).
So it depends on how you did cast, really.