posted 3 years ago

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.

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.

Ryan Bishop

Ranch Hand

Posts: 143

5

posted 3 years ago

- 1

The conversion you had originally may go horribly wrong. If I remember correctly length() returns a

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`, 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

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