Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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

double or float?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this code is basically meant to work out an olympic dive score, Then print out a bar chart, using rectangles. But, the difficulty can be between 1.2 and 3.5. So an Int wont work, but when i change it, there is problems with the rect object, loss of precision or something. And that it needs an int, but of course the bar uses score, which can be anyhing, so that can't be a Int either. I'm really stuck on how to do this, any help would be great thanks.

Heres the code




BTW you may seen things to do with rainfall, i just copied it from an old program, but theyr are just prompts and comments.
[ December 11, 2005: Message edited by: Graham Robinson ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might try using a scale like 1000 points is the distance between the bottom of the bar chart and the top of the bar chart. Use a double and round it to 2 places, then multiply is by 100 and that will give you a point in the chart.
 
Graham Robinson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but i managed to do it using an int typecast, doesn't give as accurate results, but works.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more accurate results, you might want to try Keith's suggestion. You said that the score can be anything between 1.2 and 3.5. Are you sure about this? The value of PI lies in this range. Is this a possible score value? Somehow I doubt it. In fact, there are probably MANY values between 1.2 and 3.5 that are not likely to be used as a score. However, it might be more helpful to describe what values CAN be used. For example, you can restrict the user input to only a few decimal places (2 for example). In this case, you can use an int that is really 100 times the actual score. This might take some extra processing for getting the input, but it will make drawing the bar chart much easier and more precise.

Alternatively, you can keep the float values that you have and just multiply them by 100 (or 1000 or whatever) before you graph them. The bar chart shows a relative value anyways, so such a scaling won't make any visible difference in the final result. You will still need to cast the result to an int after the multiplication, but it seems you have figured out how to do that.

Layne
 
Graham Robinson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the scores have to be within that range, as it's the dives difficulty, not the actual score. The bar chart is more of an indication, this is how i did it.

 
reply
    Bookmark Topic Watch Topic
  • New Topic