"Here are a couple of interesting things about real (fractional) types. Let's start with this example:"

float f = 3 / 5;

"This computation will result in f being equal to… zero!"

"Yeah, Rishi told me something like that."

"Oh, did he? Good. Repetition is super helpful."

"There is no error here. The division involves two integers, so the remainder is simply ignored. To stop this from happening, at least one of the two numbers in the division must be fractional."

"If one of the numbers is fractional, then the second number will first be converted to a fractional type, and then the division will be performed."

"You can solve this problem like this:"

Notation for fractional numbers:
float f = 3.0f / 5.0f;
float f = 3.0f / 5;
float f = 3 / 5.0f;

"What if the division involves variables?"

"Then we do this:"

Convert an integer variable to a fractional value:
int a = 3, b = 5;
float f = (a * 1.0f) / b;


int a = 3, b = 5;
float f = a / (b * 1.0f);


int a = 3, b = 5;
float f = (a * 1.0f) / (b * 1.0f);


 int a = 3, b = 5;
float f = (float) a / b; 

"That looks awkward. Isn't there another division operation – something more convenient?"

"Nope. This is all there is."

"OK. No problem."

undefined
10
Опрос
Objects,  10 уровень,  7 лекция
недоступен
Objects
Objects