Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The attached C code snippet only works as indicated

C++
scale = 1/2;          does not work
        scale =(float)  1/2; works
        glScalef(scale, scale, scale);


The variable "scale " is of type float, the
glScalef(scale, scale, scale);

OpenGL function expects floats. When it fails the app runs , but OpenGL just ignores the
glScalef(scale, scale, scale);
function.

Is this too simple case for a C
Automatic Type Conversion
to do its job?

That is what I am asking about.

I am not concerned that the definition
scale = 1/2;   does not work 


What I have tried:

Asked Mrs Google about
Automatic Type Conversion
, do not need any more RTFM references.
Posted
Updated 16-Apr-20 7:02am
Comments
Richard MacCutchan 16-Apr-20 12:55pm    
"do not need any more RTFM references."
Oh yes you do; how about starting with the C language specification?

Without the cast, the literal 1 is an integer, as is the literal 2. Therefore, you're performing integer division, and 1 / 2 == 0.

The integer 0 is then converted to a float and stored in your variable.

You either need to cast the numerator or the denominator to float before performing the division, or use the literal 0.5 instead.
 
Share this answer
 
really ancient, but simple understandabel

" 1 / 2" understands the compiler as "divide integer 1 with integer 2". The result is 0.

"(float) 1 / 2" understands the compiler as "divide float 1 with 2 as float ". So it get 0.5.

Try both version with 3/2. Consider reading about the Modulus operator. It is a great C++ tutorial too.
 
Share this answer
 
Comments
k5054 16-Apr-20 16:49pm    
Quote:"(float) 1 / 2" understands the compiler as "divide float 1 with 2 as float ". So it get 0.5.

More explicitly
(float)1/2
is equivalent to
(float)1/(int)2
What the compiler does here is convert (int)2 to (float)2 then performs the division.
These days though, the compiler probably converts both (float)1 and (int)2 to double, performs the division, and then performs another conversion from double to float.

In general, I think the recommendation is to use double for floating point values unless you have memory constraints or there are some issues in your problem domain that makes float a better fit. In fact, in modern C/C++ a floating point literal such as 2.71 is a double, unless suffixed with f or F for float or l or L for long double
Rick York 17-Apr-20 2:01am    
Does any compiler really support long double any more? I know Visual Studio does not. It aliases them to a plain double and sizeof returns 8.
k5054 17-Apr-20 8:27am    
gcc under linux and clang FreeBSD both report long double with size as 12. I wonder what MinGW says.

Update: that was for 32 bit executables. gcc for 64 bit reports sizeof long double as 16

Update 2: MinGW-64 also reports long double as 16 bytes.

Update 3: discussing of MSVC and MinGW long doubles here: MinGW-w64 - for 32 and 64 bit Windows / Bugs / #675 difference between way vc++ and gcc converts ascii to long double, gcc wrong[^
Vaclav_ 17-Apr-20 10:40am    
If I am reading the real replies correctly - the scale type has no bearing on automatic type conversion when scale = 1/2; I suppose an example of automatic type conversion would bring another RTFM "reply". Case closed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900