Hello.
This is a question about a problem I have with division, when trying to divide numbers like "10/3".
I get weird results where all decimals that should be generated by division are cut, so I get:
10/3 = 3.000000
But multiplication works properly:
10*0.3333333 = 0.333333
It should be noted that I have previously used Dev 4, but not been programming in C for several years.
1) a) Version of Dev: 4.9.9.2 b) Operating system: Windows XP Professional (Service Pack 2) (Swedish).
"but not been programming in C for several years" - That will get you ;)!
The problem with "double i = 10/3;" is most likely this: 10 and 3 are ints. 10/3 = 3 (int division), it is not promoted to double until AFTER the 10/3 is calculated. 10.0/3 or 10/3.0 should give a correct result - though I'd look up whether that goes first to float or double. The reason that your multiply worked OK is that you had a decimal point in the operands, so they were already at least a float (& maybe a double, I forget, again, we probably should look this up).
Hope that helps,
VetteJeep
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the fast and good response!
It did indeed work, and I get the correct output now.
Still, is it supposed to work this way in C++? I have no memory of having this problem when I was learning C and doing the standard compute-and-output-exercises.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-11-24
Yes, it has always been so for C and C++. The integer division is evaluated before the implicit cast to double. Written explicitly it is:
double i = (double)(10/3) ;
Another solution is to use a cast:
double i = (double)10 / (double)3 ;
In fact it is only necessary to cast one or the other operand for the other to be implicitly cast, but an explicit casts shows that you are aware of what you are doing and that the coercion was intentional.
Type agreement is important in C/C++ and it is well to take care, the casting an promotion rules are somewhat arcane (this example is amongst the simplest).
It is not that common to have a constant expression such as 10 / 3 (or 10.0 / 3.0), more often an expression has variables. In these circumstances you should either use an appropriate variable type in the first place or use a cast:
double x = (double)integer_denominator / (double)integer_numerator ;
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you. You are of course right. I guess I’ve never reflected over the fact that constant numbers are considered int as default and what is required to get a double/float-division. I don’t remember having any problem with it when dividing constant numbers in exercises, when learning programming, and just stumbled over it now by accident. What I was trying to do, when I realised something was wrong, was:
double time = clock() / CLOCKS_PER_SEC;
I just assumed that I would get a proper result and didn’t look in to the type of clock() or CPS. It seems that only blind luck and an over-use of “double” has saved me until now. This should, however, never be a problem for me again, as I usually know my way around the variable types. Thank you for your help, both of you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-11-25
Some things you might want to consider:
long time_ms = (clock() * 1000) / CLOCKS_PER_SEC ;
'\x64' // character constant (0x64 = 100 decimal)
100 // integer constant
100U // unsigned integer constant
100L // long constant
100UL // unsigned long constant
100LL // long long constant
100ULL // unsigned long long constant
100.0F // single precision floating point constant
100.0 // double precision floating point constant
100.0L // long double precision floating point constant
The suffixes may be upper or lower case, but lower case L can look like a 1 (one) so that is best avoided.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-11-25
Oops! Late night partying; this morning I seem to have forgotten what a C++ \ comment delimiter looks like!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I simply store my time as “clocks” in an int variable, and convert to seconds only to show the time on screen and for some approximations.
I’d still like to consider the things you mention, but I’m not quite sure what to make of it. Are you saying that I could use:
“variableL” interchangeably with “long(variable)”
or that it works that way for constant numbers, so that“100L” is the same as “long(100)”? Or am I right to assume I’m totally missing the point?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-11-25
> Are you saying that I could use: “variableL” interchangeably with “long(variable)”
No definitely not. These suffixes are for literal constants only. It is not the same as a cast, although it may have much the same effect. The use of the suffix will be dealt with entirely at compile time; it specifies a constant of a particular type - there is no conversion, it is the correct type in the first instance. A cast on the other hand specifies a conversion and may in principle have a run-time overhead. Whether it does or not is probably dependent upon the compiler and the optimisations settings used, as well as the nature of the expression being cast.
My point in posting that information was in response to your comment "[...]constant numbers are considered int as default[...]", to show how you can specify constants of other types. An important point to note is that 1.0 is a double whereas 1.0f is a float.
Hello.
This is a question about a problem I have with division, when trying to divide numbers like "10/3".
I get weird results where all decimals that should be generated by division are cut, so I get:
10/3 = 3.000000
But multiplication works properly:
10*0.3333333 = 0.333333
It should be noted that I have previously used Dev 4, but not been programming in C for several years.
1) a) Version of Dev: 4.9.9.2 b) Operating system: Windows XP Professional (Service Pack 2) (Swedish).
2) Simple example program:
(variabelfel 01.cpp)
include <iomanip>
int main()
{
double i=10/3, j=100.3333333;
printf("10/3 = %f \n100.3333333 = %f\n\n",i,j);
system("PAUSE");
return 0;
}
Output of program:
"
10/3 = 3.000000
10*0.3333333 = 0.333333
Tryck på valfri tangent för att fortsätta... _
"
3)Compiler log:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Daniel\Mina dokument\Programmering\variabelfel 01.cpp" -o "C:\Documents and Settings\Daniel\Mina dokument\Programmering\variabelfel 01.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
Execution terminated
That makes perfect sense, thank you Clifford.
I'm really beginning to like this forum!
It's of course supposed to be "10*0.3333333 = 3.333333" in both places where is says "0.333333.
As said, multiplication works properly.
"but not been programming in C for several years" - That will get you ;)!
The problem with "double i = 10/3;" is most likely this: 10 and 3 are ints. 10/3 = 3 (int division), it is not promoted to double until AFTER the 10/3 is calculated. 10.0/3 or 10/3.0 should give a correct result - though I'd look up whether that goes first to float or double. The reason that your multiply worked OK is that you had a decimal point in the operands, so they were already at least a float (& maybe a double, I forget, again, we probably should look this up).
Hope that helps,
VetteJeep
Thanks for the fast and good response!
It did indeed work, and I get the correct output now.
Still, is it supposed to work this way in C++? I have no memory of having this problem when I was learning C and doing the standard compute-and-output-exercises.
Yes, it has always been so for C and C++. The integer division is evaluated before the implicit cast to double. Written explicitly it is:
double i = (double)(10/3) ;
Another solution is to use a cast:
double i = (double)10 / (double)3 ;
In fact it is only necessary to cast one or the other operand for the other to be implicitly cast, but an explicit casts shows that you are aware of what you are doing and that the coercion was intentional.
Type agreement is important in C/C++ and it is well to take care, the casting an promotion rules are somewhat arcane (this example is amongst the simplest).
It is not that common to have a constant expression such as 10 / 3 (or 10.0 / 3.0), more often an expression has variables. In these circumstances you should either use an appropriate variable type in the first place or use a cast:
double x = (double)integer_denominator / (double)integer_numerator ;
Clifford
Thank you. You are of course right. I guess I’ve never reflected over the fact that constant numbers are considered int as default and what is required to get a double/float-division. I don’t remember having any problem with it when dividing constant numbers in exercises, when learning programming, and just stumbled over it now by accident. What I was trying to do, when I realised something was wrong, was:
double time = clock() / CLOCKS_PER_SEC;
I just assumed that I would get a proper result and didn’t look in to the type of clock() or CPS. It seems that only blind luck and an over-use of “double” has saved me until now. This should, however, never be a problem for me again, as I usually know my way around the variable types. Thank you for your help, both of you.
Some things you might want to consider:
long time_ms = (clock() * 1000) / CLOCKS_PER_SEC ;
'\x64' // character constant (0x64 = 100 decimal)
100 // integer constant
100U // unsigned integer constant
100L // long constant
100UL // unsigned long constant
100LL // long long constant
100ULL // unsigned long long constant
100.0F // single precision floating point constant
100.0 // double precision floating point constant
100.0L // long double precision floating point constant
The suffixes may be upper or lower case, but lower case L can look like a 1 (one) so that is best avoided.
Clifford
Oops! Late night partying; this morning I seem to have forgotten what a C++ \ comment delimiter looks like!
I simply store my time as “clocks” in an int variable, and convert to seconds only to show the time on screen and for some approximations.
I’d still like to consider the things you mention, but I’m not quite sure what to make of it. Are you saying that I could use:
“variableL” interchangeably with “long(variable)”
or that it works that way for constant numbers, so that“100L” is the same as “long(100)”? Or am I right to assume I’m totally missing the point?
> Are you saying that I could use: “variableL” interchangeably with “long(variable)”
No definitely not. These suffixes are for literal constants only. It is not the same as a cast, although it may have much the same effect. The use of the suffix will be dealt with entirely at compile time; it specifies a constant of a particular type - there is no conversion, it is the correct type in the first instance. A cast on the other hand specifies a conversion and may in principle have a run-time overhead. Whether it does or not is probably dependent upon the compiler and the optimisations settings used, as well as the nature of the expression being cast.
My point in posting that information was in response to your comment "[...]constant numbers are considered int as default[...]", to show how you can specify constants of other types. An important point to note is that 1.0 is a double whereas 1.0f is a float.
See here: http://msdn2.microsoft.com/en-us/library/c70dax92(VS.71).aspx for a more comprehensive discussion of literal constants.
Clifford