reset password
Author Message
bliu14
Posts: 10
Posted 04:04 Mar 12, 2017 |

I am having some trouble with my outputs for both conversions in HW07P05. Both have small rounding errors that make my answers deviate slightly from the sample answers.

 

In converting binary to decimal, my output is in scientific notation. How can this be fixed to display nonscientific notation?

Should be:  11111101100100010110111000001110111110100101001100010101110100 in decimal is 4567876543456789876 

Mine:          11111101100100010110111000001110111110100101001100010101110100 in decimal is 4.5678765434567895E18

 

In converting decimal to binary, there are also some rounding errors towards the end of the binary string output, and I suspect this is due to rounding in class casting. How can this be fixed to be more precise?

Should be:  4567876543456789876 in binary is 11111101100100010110111000001110111110100101001100010101110100

Mine:          4.56787654345679E18  in binary is 11111101100100010110111000001110111110100101001100011000000000

 

Are these errors notable enough to fix, or are they minor rounding errors I shouldn't worry about? Anyone else having the same results? Thanks!

fengus97
Posts: 18
Posted 04:09 Mar 12, 2017 |

Maybe try using a different data type? i know INT is not big enough for some of the examples in the sample output.

 

not too sure about the second part though 

fengus97
Posts: 18
Posted 04:12 Mar 12, 2017 |

Follow-up: just tried to convert 4.56787654345679E18 and i got an error..... Are we supposed to accommodate values that are so large they have exponents? surprise

kknaur
Posts: 540
Posted 12:18 Mar 12, 2017 |

There should not be any rounding errors since you should be working with only integers.

bliu14
Posts: 10
Posted 14:37 Mar 12, 2017 |

I originally had some classes as doubles to accomodate for the large example input, but I changed them all to int (except for the binary string). However, according to the textbook, the int class can only store values up to 2147483647, while the example input in the homework was 4567876543456789876. I get the "Exception in thread..." error when running the program due to incompatible class. My input of 4567876543456789876 was larger than the capacity of the int class.

How should I approach the incompatible class issue? Thanks!

kknaur
Posts: 540
Posted 14:46 Mar 12, 2017 |

Be careful with your terminology.  I am not sure what you are referring to when you say "classes".  A class is either a reference variable type or the construct where you define your code (public class MyProgram). 

Also review all the various primitive data types.  int isn't the only type that can represent integers...