• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bit lost with doing a ratio

 
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Thanks for any advice.

 
Greenhorn
Posts: 22
Mac Netbeans IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac Netbeans IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The conversion you saw was probbaly a personal choice or it could have been a requirement by a client or instructor.
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the cow, whoever gave it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic