• 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

Scala sqrt function returns different values

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are these two results different?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are doing integer division, where 1 / 5 == 0.

Try sqrt((1.0/5) * 10)
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool! I would have expected that it promoted itself to a double in that case.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it doesn't. What would you expect the rules to be?

You would not want to always automatically convert to double when you use division with integers, because sometimes integer division is exactly what you want.

In Scala this works in the same way as in Java, C++ and other languages, this is not some strange quirk specific to Scala.
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Joe on this one.

In my opinion - even though I'm completely used to Java's/C++'s etc. treatment of integer division, it's still utterly useless.

Scala has mostly left bullcrap like this behind. Don't know why the language would retain this particular bug.

If you want integer division it should be something you have to explicitly do somehow. The majority of the time when programmers use division its just like this example where you want the result as a float.

My two cents.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Shipp wrote:
If you want integer division it should be something you have to explicitly do somehow. The majority of the time when programmers use division its just like this example where you want the result as a float.



I wouldn't agree with that. The majority of times I use integer division I want integer division. If I want floating point division the chances are I've got floating point numbers to begin with.

I'd expect Scala to behave like every other statically typed language I'm familiar with. It isn't just Java/C/C++.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

Scott Shipp wrote:
If you want integer division it should be something you have to explicitly do somehow. The majority of the time when programmers use division its just like this example where you want the result as a float.



I wouldn't agree with that. The majority of times I use integer division I want integer division. If I want floating point division the chances are I've got floating point numbers to begin with.

I'd expect Scala to behave like every other statically typed language I'm familiar with. It isn't just Java/C/C++.



I agree in the case where your dividing x / y and x > y. But would you agree that 100% of the time when you divide x / y and x < y, you want a float result? Or do you really want zero? I may not be grokking something obvious as its been a really long week but when would you ever have essentially a fraction (as in 1/5) and want 0?

It's especially egregious in the above given example where the final result is a float anyway.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be horrible and impossible to make the rules of the language so that if x > y, the type of the result of x / y is Int, and when x < y, the result is Double. In general you don't know the values of x and y until runtime, so if the type of the expression x / y depends on the values of x and y then it is impossible to know the type at compile-time.

It would perhaps be good if the compiler would give you a warning if you do x / y where x and y are compile-time constants and x < y.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To each his own. Maybe those of us who like this idea can make an implicit to do this for us.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic