Real types

New Java Syntax
Level 8 , Lesson 7
Available

"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."

Comments (10)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
larsintech Level 14, Switzerland Expert
25 November 2024
The last example (float) is actually casting an int to a float. I hope casting will be explained in more depth.
marcus Simoes Level 13, Dortmund, Germany
14 November 2020
i think u choose float instead double if u want to spare memory and if the result must not be that exact.
Andrei Level 41
17 November 2020
exactly, float occupies less bytes in the memory making your program more efficient. somebody posted this nice pictures a few lessons ago.
Daniel Tinsley Level 22, United States, United States
12 October 2019
This is one area where Python 3 is better. / is used for float division, so two integers can be divided and the result is a float, while // is used for floor division, which is similar to the / operator in Java.
Krisztian Level 24, Budapest, Hungary
2 June 2019
I don't get why the fractional examples are with floats. A couple lessons before has been said, that we should use always double for that. :/
Krisztian Level 24, Budapest, Hungary
29 June 2019
Yeah, the double is more accurate than float which is came from their size in the memory. I met with programming at science field (physics) so I always used and I'm always gonna use double for fraction numbers. I just can't imagine that this 4 byte difference is going to matter at a big project that I'll have to use float instead double.
Thomas Sixberry Level 16, Rochester Hills, United States
4 February 2020
It is interesting isn't it. Wonder if they are cutting corners for Memory purposes.
Devonte A Level 18, Rubery, United Kingdom
22 June 2020
Correct, probably just best to use double.
Jonaskinny Level 25, Redondo Beach, United States
7 February 2022
@Krisztian re 'I just can't imagine that this 4 byte difference is going to matter at a big project' turns out many times the bigger the project ($) the more performance and space matters. One example would be where precision via float suffices, and out-of-process things like rules engines processing the system data, and it makes economic sense to have a developer go through the code and get x process to run in under y seconds/minutes etc. because there are z dollars on the line if you cant hit the target. Go to any github repository, look in the /~util~ and find a general utility class. The more impressive the project the more you are likely to see things like bit-wise calculations etc. Tiny can be Huge.
abhishe_kira Level 18, India Expert
14 June 2023
Sir, you know I read each and every comment that you did in any lesson. Thank you for your generosity.