programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Bit lost with doing a ratio

Ranch Hand
Posts: 143
5
Hi,

I'm doing a problem where I create two files (one in Word and one in notepad) with the same movie quote. I then have to create an application
saying if they exist and if they do show their file sizes and ratio of them. I think I'm fine with the file sizes but I don't know
how to get a ratio other than dividing one by the other, in which case quote1/quote2 gives a 0 and the reverse gives 44,
which leads to me not having any idea how to do an actual ratio like 3:2 or whatever.
Is this even remotely the right way to go about it? The issue is at line 17.

Greenhorn
Posts: 22
• 1
Ryan,

Are you sure the problem actaully wants you to divide the lengths and not just display them like quote1.length : quote2.length? Commonly, a ratio represents a comparison of the size of two values.

Steve

Ryan Bishop
Ranch Hand
Posts: 143
5
Yeah, I think you're right. I saw a few examples browsing the web and that seems to be how they do it.

Is it standard to convert the length to kilobytes? I saw one person do something like below rather
than the length of the original:

Steve Kedzie
Greenhorn
Posts: 22
• 1
The conversion you saw was probbaly a personal choice or it could have been a requirement by a client or instructor.

Marshal
Posts: 56610
172
• 1
The conversion you had originally may go horribly wrong. If I remember correctly length() returns a long, so you are using integer arithmetic; if you divide 387465983746L by 387465983747L, you get zero. If you divide the other way round, you get one; you also get one from dividing 774931967491L by 387465983746L.

I would suggest for your kB calculations you use the % operator too. Don't use 1024 which is a “magic number”; it equals 0x400.That will give you the size inwhole numbers of kB but round up to return 1 from a 0.1kB file. If you are using % with an exact power of 2 you can use
((quote1size & BYTES_IN_KB - 1) == 0 ? 0 : 1)
instead, if you understand the strange‑looking syntax.

You may suffer imprecision for large files because the precision of a long is greater than a double's for large values of long.

Campbell Ritchie
Marshal
Posts: 56610
172
Thank you for the cow, whoever gave it

 Don't get me started about those stupid light bulbs.