• 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

Java truncating values....not sure how to remedy

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi guys

If I was to enter 2, 5, -1 this should result in 2+5/2=3.5 but at present, the value of avg is denoted as 3.0 instead of 3.5. I am aware that Java truncates downwards (heading toward 0) but I am not sure as to how I would ensure greater degree of precision of the decimal points? I thought the utilisation of double with its 8 bit, 16 decimal point precision vs float's more limited 4 bit was sufficient but obviously I am missing some crucial piece of the puzzle here (though regrettably, not 100% what exactly that so happens to be).
 
tony narloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually managed to solve it. What was happening was I was getting problems where I needed values to be of the int type in one segment of code, but a double in another.

I remedied this by virtue of the following:



Which gave me the best of both worlds (AND solved my initial problem) hope this helps someone else too!
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done finding the problem

There is a quicker way to solve it
double average = (double)sum / count;
Check the relative precedences of cast and divide first.

Don't use underscores_ in variable names. Don't include the type in a variable name if at all possible. Search for “Hungarian notation” for more details.

What does line 21 mean? Does it mean your loop is running once too often? I shall introduce you to some nasty‑looking syntax which will probably solve that problem. One pair of () is needed because the precedence of = is very low, and you need to do the assignment before the inequality test.If you are using Scanners however, there is a better way still to do that:-orIf you enter numbers like this:-

1 2 3 69 34586 723 0 -8345 -1 stop

...you will get nine numbers entered and the “stop” will terminate the loop.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic