Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print result of a function

How can I change this function in Scala to be able, when running it on IntelliJ IDEA, to see the result?

object poisson_sample_size extends App {

  def main(theta1: Double, theta2: Double): Double = {

    val theta1 = 0.0025
    val theta2 = 0.0030

    val num = 4
    val den = ((theta1 + theta2) / 2) - math.sqrt(theta1 * theta2)

    val x = num / den
    println(x: Double)
  }
}

I would like just to check the result is what I'm expecting it to be. I'm not sure it's error proof considering I just started learning Scala.

I've trying to attribute the result of (num / den) to a variable and then print the variable itself but it doesn't do what I was expecting.

like image 806
Gianluca Avatar asked Dec 25 '22 21:12

Gianluca


1 Answers

Try changing your code to something like this:

object poisson_sample_size extends App {
  val t1 = 0.0025
  val t2 = 0.0030

  val result = calculate(t1, t2)
  println(result)

  def calculate(theta1: Double, theta2: Double): Double = {         
    val num = 4
    val den = ((theta1 + theta2) / 2) - math.sqrt(theta1 * theta2)

    num / den       
  }
}

By making your object extend App, you get a main method for free. Then, using main is not a good idea for your custom calculation method as it creates confusion with a real main method, so I changed it to calculate. Then, you need to make sure your calculate method is being called, which is probably the real reason why you don't see anything getting printed in your current example.

like image 76
cmbaxter Avatar answered Jan 02 '23 21:01

cmbaxter