• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Doubt in math.round

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



The following is the output for the above program
Round f4 is -5
Round f5 is 6
Round f6 is -5
Round f7 is 5
My doubt is how -5 became the output for f4?
can you please tell me
 
Sheriff
Posts: 22768
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a value's decimal part is exactly 0.5, it's rounded up (as specified in the Javadoc comments). For negative numbers this means towards 0. For positive numbers this means away from 0.
 
Marshal
Posts: 78651
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote: . . . as specified in the Javadoc comments . . .

It isn’t at all clear in the Javadoc comments. If you find the Java6 version, it says

Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

(long)Math.floor(a + 0.5d)

. . . but the Java7 version says

with ties rounding up.

I think the old version was much easier to understand.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the Major difference between Math.Round and Math.Floor Functions?
 
Marshal
Posts: 27987
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saahil gupta wrote:What is the Major difference between Math.Round and Math.Floor Functions?



0 or 1.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saahil gupta wrote:What is the Major difference between Math.Round and Math.Floor Functions?



When you read the docs for both, what part didn't you understand?

When you executed round() and floor() on 1.0, 1.1, 1.4, 1.5, 1.6, and 1.9, which output did not make sense to you?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saahil gupta wrote:What is the Major difference between Math.Round and Math.Floor Functions?


directly from the API:

round:
Returns the closest long to the argument.

Floor:
Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think the old version was much easier to understand.


I think the new version is much easier to understand; moreover the old version is decidedly too much information. Documentation should tell me in no uncertain terms what result I will get, to be sure; but it should not lay bare the procedure employed to reach that result.
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic