Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:53:54+00:00 2026-06-15T12:53:54+00:00

For some reason (that escapes me), Scala math library does not have a pow

  • 0

For some reason (that escapes me), Scala math library does not have a pow-function for integers, but only for Doubles.

I need a square function for integers and was figuring what might be the usual way to do this in Scala.

object TestX extends App {

  def pow2(v: Int)= v*v

  //class MyRichInt( val v: Int ) {
  //  def ² : Int = v*v      // says: "illegal character" for UTF-8 power-of-two
  //}

  println( pow2(42) )
  //println( 42² )
  println( math.pow(42,2).toInt )
}

I was surprised to see that the ‘²’ character is not liked by Scala. Maybe it’s taken to be a number? Usually all kinds of weird Unicode values are valid and using 42² in code would, indeed, be fancy.

Never mind. Should I shut up and just start using my own pow2 function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:53:54+00:00Added an answer on June 15, 2026 at 12:53 pm

    Yes, use your own pow2. If you need higher powers, you probably won’t have room in an Int anyway. Consider using BigInt.pow:

    scala> BigInt(40).pow(40)
    res0: scala.math.BigInt = 12089258196146291747061760000000000000000000000000000000000000000
    

    Of course, if you need not N2 but 2N, just use shifts. (1 << k = 2k) These work with BigInt also.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some ActionScript3 code that for some reason only crashes in a stack
For some reason that I don't understand, video websites do not play when using
For some reason I have always assumed that most of the time a variable
For some reason this program is saying that 'switch' is not defined. What is
I know that this may be an amateur question but for some reason I
For some reason the AppModel->updateAll() method does not escape data passed to it. Looking
I have a PHP script that does some validation, and if the validation is
For some reason there's a variable called d that is defined immediately after I
For some reason I'm getting an error that basically states that it can't find
For some reason i don't like any of the themes that are on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.