Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/scala.math

scala.math

packagescala.math

The package objectscala.math contains methods for performing basic numeric operations such as elementary exponential, logarithmic, root and trigonometric functions.

All methods forward tojava.lang.Math unless otherwise noted.

Attributes

See also

Members list

Grouped members

Mathematical Constants

finalvalE: 2.718281828459045d

TheDouble value that is closer than any other toe, the base of the natural logarithms.

TheDouble value that is closer than any other toe, the base of the natural logarithms.

Attributes

Source
package.scala
finalvalPi: 3.141592653589793d

TheDouble value that is closer than any other topi, the ratio of the circumference of a circle to its diameter.

TheDouble value that is closer than any other topi, the ratio of the circumference of a circle to its diameter.

Attributes

Source
package.scala

Minimum and Maximum

Find the min or max of two numbers. Note:scala.collection.IterableOnceOps has min and max methods which determine the min or max of a collection.

defmax(x:Int,y:Int):Int

Attributes

Source
package.scala
defmax(x:Long,y:Long):Long

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala
defmin(x:Int,y:Int):Int

Attributes

Source
package.scala
defmin(x:Long,y:Long):Long

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Rounding

Attributes

Source
package.scala

Attributes

Source
package.scala

Returns theDouble value that is closest in value to the argument and is equal to a mathematical integer.

Returns theDouble value that is closest in value to the argument and is equal to a mathematical integer.

Value parameters

x

aDouble value

Attributes

Returns

the closest floating-point value to a that is equal to a mathematical integer.

Source
package.scala

There is no reason to round aLong, but this method prevents unintended conversion toFloat followed by rounding toInt.

There is no reason to round aLong, but this method prevents unintended conversion toFloat followed by rounding toInt.

Attributes

Note

Does not forward tojava.lang.Math

Deprecated
[Since version 2.11.0]This is an integer type; there is no reason to round it. Perhaps you meant to call this with a floating-point value?
Source
package.scala

Returns the closestInt to the argument.

Returns the closestInt to the argument.

Value parameters

x

a floating-point value to be rounded to aInt.

Attributes

Returns

the value of the argument rounded to the nearestInt value.

Source
package.scala

Returns the closestLong to the argument.

Returns the closestLong to the argument.

Value parameters

x

a floating-point value to be rounded to aLong.

Attributes

Returns

the value of the argument rounded to the nearestlong value.

Source
package.scala

Scaling

Scaling with rounding guarantees

defscalb(d:Double,scaleFactor:Int):Double

Attributes

Source
package.scala
defscalb(f:Float,scaleFactor:Int):Float

Attributes

Source
package.scala

Exponential and Logarithmic

Returns Euler's numbere raised to the power of aDouble value.

Returns Euler's numbere raised to the power of aDouble value.

Value parameters

x

the exponent to raisee to.

Attributes

Returns

the valueea, wheree is the base of the natural logarithms.

Source
package.scala

Returnsexp(x) - 1.

Returnsexp(x) - 1.

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Returns the natural logarithm of aDouble value.

Returns the natural logarithm of aDouble value.

Value parameters

x

the number to take the natural logarithm of

Attributes

Returns

the valuelogₑ(x) wheree is Eulers number

Source
package.scala

Returns the base 10 logarithm of the givenDouble value.

Returns the base 10 logarithm of the givenDouble value.

Attributes

Source
package.scala

Returns the natural logarithm of the sum of the givenDouble value and 1.

Returns the natural logarithm of the sum of the givenDouble value and 1.

Attributes

Source
package.scala

Returns the value of the first argument raised to the power of the second argument.

Returns the value of the first argument raised to the power of the second argument.

Value parameters

x

the base.

y

the exponent.

Attributes

Returns

the valuexy.

Source
package.scala

Trigonometric

Arguments in radians

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Angular Measurement Conversion

Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

Value parameters

x

angle, in radians

Attributes

Returns

the measurement of the anglex in degrees.

Source
package.scala

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Value parameters

x

an angle, in degrees

Attributes

Returns

the measurement of the anglex in radians.

Source
package.scala

Hyperbolic

Returns the hyperbolic cosine of the givenDouble value.

Returns the hyperbolic cosine of the givenDouble value.

Attributes

Source
package.scala

Returns the hyperbolic sine of the givenDouble value.

Returns the hyperbolic sine of the givenDouble value.

Attributes

Source
package.scala

Returns the hyperbolic tangent of the givenDouble value.

Returns the hyperbolic tangent of the givenDouble value.

Attributes

Source
package.scala

Absolute Values

Determine the magnitude of a value by discarding the sign. Results are >= 0.

defabs(x:Int):Int

Attributes

Source
package.scala
defabs(x:Long):Long

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Signs

Forsignum extract the sign of a value. Results are -1, 0 or 1. Note thesignum methods are not pure forwarders to the Java versions. In particular, the return type ofjava.lang.Long.signum isInt, but here it is widened toLong so that each overloaded variant will return the same numeric type it is passed.

defcopySign(magnitude:Double,sign:Double):Double

Attributes

Source
package.scala
defcopySign(magnitude:Float,sign:Float):Float

Attributes

Source
package.scala
defsignum(x:Int):Int

Attributes

Note
Source
package.scala

Attributes

Note

Forwards tojava.lang.Long

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Root Extraction

Returns the cube root of the givenDouble value.

Returns the cube root of the givenDouble value.

Value parameters

x

the number to take the cube root of

Attributes

Returns

the value ∛x

Source
package.scala

Returns the square root of aDouble value.

Returns the square root of aDouble value.

Value parameters

x

the number to take the square root of

Attributes

Returns

the value √x

Source
package.scala

Polar Coordinates

Converts rectangular coordinates(x, y) to polar(r, theta).

Converts rectangular coordinates(x, y) to polar(r, theta).

Value parameters

x

the ordinate coordinate

y

the abscissa coordinate

Attributes

Returns

thetheta component of the point(r, theta) in polar coordinates that corresponds to the point(x, y) in Cartesian coordinates.

Source
package.scala

Returns the square root of the sum of the squares of both givenDouble values without intermediate underflow or overflow.

Returns the square root of the sum of the squares of both givenDouble values without intermediate underflow or overflow.

Ther component of the point(r, theta) in polar coordinates that corresponds to the point(x, y) in Cartesian coordinates.

Attributes

Source
package.scala

Unit of Least Precision

Returns the size of an ulp of the givenDouble value.

Returns the size of an ulp of the givenDouble value.

Attributes

Source
package.scala

Returns the size of an ulp of the givenFloat value.

Returns the size of an ulp of the givenFloat value.

Attributes

Source
package.scala

Pseudo Random Number Generation

Returns aDouble value with a positive sign, greater than or equal to0.0 and less than1.0.

Returns aDouble value with a positive sign, greater than or equal to0.0 and less than1.0.

Attributes

Source
package.scala

Exact Arithmetic

Integral addition, multiplication, stepping and conversion throwing ArithmeticException instead of underflowing or overflowing

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Modulus and Quotient

Calculate quotient values by rounding to negative infinity

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Adjacent Floats

defnextAfter(start:Double,direction:Double):Double

Attributes

Source
package.scala
defnextAfter(start:Float,direction:Double):Float

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Attributes

Source
package.scala

Type members

Classlikes

Attributes

Companion
class
Source
BigDecimal.scala
Supertypes
classObject
traitMatchable
classAny
Self type
finalclassBigDecimal(valbigDecimal:BigDecimal,valmc:MathContext) extendsScalaNumber,ScalaNumericConversions,Serializable,Ordered[BigDecimal]

BigDecimal represents decimal floating-point numbers of arbitrary precision.

BigDecimal represents decimal floating-point numbers of arbitrary precision. By default, the precision approximately matches that of IEEE 128-bit floating point numbers (34 decimal digits,HALF_EVEN rounding mode). Within the range of IEEE binary128 numbers,BigDecimal will agree withBigInt for both equality and hash codes (and will agree with primitive types as well). Beyond that range--numbers with more than 4934 digits when written out in full--thehashCode ofBigInt andBigDecimal is allowed to diverge due to difficulty in efficiently computing both the decimal representation inBigDecimal and the binary representation inBigInt.

When creating aBigDecimal from aDouble orFloat, care must be taken as the binary fraction representation ofDouble andFloat does not easily convert into a decimal representation. Three explicit schemes are available for conversion.BigDecimal.decimal will convert the floating-point number to a decimal text representation, and build aBigDecimal based on that.BigDecimal.binary will expand the binary fraction to the requested or default precision.BigDecimal.exact will expand the binary fraction to the full number of digits, thus producing the exact decimal value corresponding to the binary fraction of that floating-point number.BigDecimal equality matches the decimal expansion ofDouble:BigDecimal.decimal(0.1) == 0.1. Note that since0.1f != 0.1, the same is not true forFloat. Instead,0.1f == BigDecimal.decimal((0.1f).toDouble).

To test whether aBigDecimal number can be converted to aDouble orFloat and then back without loss of information by using one of these methods, test withisDecimalDouble,isBinaryDouble, orisExactDouble or the correspondingFloat versions. Note thatBigInt'sisValidDouble will agree withisExactDouble, not theisDecimalDouble used by default.

BigDecimal uses the decimal representation of binary floating-point numbers to determine equality and hash codes. This yields different answers than conversion betweenLong andDouble values, where the exact form is used. As always, since floating-point is a lossy representation, it is advisable to take care when assuming identity will be maintained across multiple conversions.

BigDecimal maintains aMathContext that determines the rounding that is applied to certain calculations. In most cases, the value of theBigDecimal is also rounded to the precision specified by theMathContext. To create aBigDecimal with a different precision than itsMathContext, usenew BigDecimal(new java.math.BigDecimal(...), mc). Rounding will be applied on those mathematical operations that can dramatically change the number of digits in a full representation, namely multiplication, division, and powers. The left-hand argument'sMathContext always determines the degree of rounding, if any, and is the one propagated through arithmetic operations that do not apply rounding themselves.

Attributes

Companion
object
Source
BigDecimal.scala
Supertypes
classScalaNumber
classNumber
classObject
traitMatchable
classAny
Show all
objectBigInt

Attributes

Companion
class
Source
BigInt.scala
Supertypes
classObject
traitMatchable
classAny
Self type
BigInt.type

A type with efficient encoding of arbitrary integers.

A type with efficient encoding of arbitrary integers.

It wrapsjava.math.BigInteger, with optimization for small values that can be encoded in aLong.

Attributes

Companion
object
Source
BigInt.scala
Supertypes
classScalaNumber
classNumber
classObject
traitMatchable
classAny
Show all
traitEquiv[T] extendsSerializable

A trait for representing equivalence relations.

A trait for representing equivalence relations. It is important to distinguish between a type that can be compared for equality or equivalence and a representation of equivalence on some type. This trait is for representing the latter.

Anequivalence relation is a binary relation on a type. This relation is exposed as theequiv method of theEquiv trait. The relation must be:

  1. reflexive:equiv(x, x) == true for any x of typeT.

  2. symmetric:equiv(x, y) == equiv(y, x) for anyx andy of typeT.

  3. transitive: ifequiv(x, y) == true andequiv(y, z) == true, thenequiv(x, z) == true for anyx,y, andz of typeT.

Attributes

Companion
object
Source
Equiv.scala
Supertypes
classAny
Known subtypes
objectBigDecimal
objectBigInt
objectBoolean
objectByte
objectChar
traitIeeeEquiv
objectIeeeEquiv
traitIeeeEquiv
objectIeeeEquiv
objectInt
objectLong
objectShort
objectString
objectSymbol
objectUnit
traitOrdering[T]
traitNumeric[T]
traitFractional[T]
traitIntegral[T]
objectBigDecimal
objectBigInt
objectBoolean
objectByte
traitCachedReverse[T]
objectInt
objectChar
objectLong
objectShort
objectString
objectSymbol
objectUnit
Show all
objectEquiv extendsLowPriorityEquiv

Attributes

Companion
trait
Source
Equiv.scala
Supertypes
Self type
Equiv.type
traitFractional[T] extendsNumeric[T]

Attributes

Companion
object
Source
Fractional.scala
Supertypes
traitNumeric[T]
traitOrdering[T]
traitEquiv[T]
traitComparator[T]
classObject
traitMatchable
classAny
Show all
Known subtypes

Attributes

Companion
trait
Source
Fractional.scala
Supertypes
classObject
traitMatchable
classAny
Self type
traitIntegral[T] extendsNumeric[T]

Attributes

Companion
object
Source
Integral.scala
Supertypes
traitNumeric[T]
traitOrdering[T]
traitEquiv[T]
traitComparator[T]
classObject
traitMatchable
classAny
Show all
Known subtypes
objectIntegral

Attributes

Companion
trait
Source
Integral.scala
Supertypes
classObject
traitMatchable
classAny
Self type

Attributes

Source
Equiv.scala
Supertypes
classObject
traitMatchable
classAny
Known subtypes
objectEquiv
Self type
Equiv.type

Attributes

Source
Ordering.scala
Supertypes
classObject
traitMatchable
classAny
Known subtypes
objectOrdering
objectNumeric

Attributes

Companion
trait
Source
Numeric.scala
Supertypes
classObject
traitMatchable
classAny
Self type
Numeric.type
traitOrdered[A] extendsComparable[A]

A trait for data that have a single, natural ordering.

A trait for data that have a single, natural ordering. Seescala.math.Ordering before using this trait for more information about whether to usescala.math.Ordering instead.

Classes that implement this trait can be sorted withscala.util.Sorting and can be compared with standard comparison operators (e.g. > and <).

Ordered should be used for data with a single, natural ordering (like integers) while Ordering allows for multiple ordering implementations. An Ordering instance will be implicitly created if necessary.

scala.math.Ordering is an alternative to this trait that allows multiple orderings to be defined for the same type.

scala.math.PartiallyOrdered is an alternative to this trait for partially ordered data.

For example, create a simple class that implementsOrdered and then sort it withscala.util.Sorting:

case class OrderedClass(n:Int) extends Ordered[OrderedClass] {def compare(that: OrderedClass) =  this.n - that.n}val x = Array(OrderedClass(1), OrderedClass(5), OrderedClass(3))scala.util.Sorting.quickSort(x)x

It is important that theequals method for an instance ofOrdered[A] be consistent with the compare method. However, due to limitations inherent in the type erasure semantics, there is no reasonable way to provide a default implementation of equality for instances ofOrdered[A]. Therefore, if you need to be able to use equality on an instance ofOrdered[A] you must provide it yourself either when inheriting or instantiating.

It is important that thehashCode method for an instance ofOrdered[A] be consistent with thecompare method. However, it is not possible to provide a sensible default implementation. Therefore, if you need to be able compute the hash of an instance ofOrdered[A] you must provide it yourself either when inheriting or instantiating.

Attributes

See also
Companion
object
Source
Ordered.scala
Supertypes
traitComparable[A]
classObject
traitMatchable
classAny
Known subtypes
classDeadline
classDuration
classInfinite
classEnumeration.this.Value
classEnumeration.this.Val
classBigInt
traitOrderedProxy[T]
classRichFloat
classRichInt
traitIntegralProxy[T]
classRichChar
classRichLong
classRichByte
classRichShort
Show all
objectOrdered

Attributes

Companion
trait
Source
Ordered.scala
Supertypes
classObject
traitMatchable
classAny
Self type
Ordered.type

Ordering is a trait whose instances each represent a strategy for sorting instances of a type.

Ordering is a trait whose instances each represent a strategy for sorting instances of a type.

Ordering's companion object defines many implicit objects to deal with subtypes ofAnyVal (e.g.Int,Double),String, and others.

To sort instances by one or more member variables, you can take advantage of these built-in orderings usingOrdering.by andOrdering.on:

import scala.util.Sortingval pairs = Array(("a", 5, 2), ("c", 3, 1), ("b", 1, 3))// sort by 2nd elementSorting.quickSort(pairs)(Ordering.by[(String, Int, Int), Int](_._2))// sort by the 3rd element, then 1stSorting.quickSort(pairs)(Ordering[(Int, String)].on(x => (x._3, x._1)))

AnOrdering[T] is implemented by specifying thecompare method,compare(a: T, b: T): Int, which decides how to order two instancesa andb. Instances ofOrdering[T] can be used by things likescala.util.Sorting to sort collections likeArray[T].

For example:

import scala.util.Sortingcase class Person(name:String, age:Int)val people = Array(Person("bob", 30), Person("ann", 32), Person("carl", 19))// sort by ageobject AgeOrdering extends Ordering[Person] { def compare(a:Person, b:Person) = a.age.compare(b.age)}Sorting.quickSort(people)(AgeOrdering)

This trait andscala.math.Ordered both provide this same functionality, but in different ways. A typeT can be given a single way to order itself by extendingOrdered. UsingOrdering, this same type may be sorted in many other ways.Ordered andOrdering both provide implicits allowing them to be used interchangeably.

You canimport scala.math.Ordering.Implicits._ to gain access to other implicit orderings.

Attributes

See also
Companion
object
Source
Ordering.scala
Supertypes
traitEquiv[T]
traitComparator[T]
classObject
traitMatchable
classAny
Show all
Known subtypes
traitNumeric[T]
traitFractional[T]
traitIntegral[T]
objectBigDecimal
objectBigInt
objectBoolean
objectByte
traitCachedReverse[T]
objectInt
objectChar
objectLong
objectShort
objectString
objectSymbol
objectUnit
Show all
Self type

This is the companion object for thescala.math.Ordering trait.

This is the companion object for thescala.math.Ordering trait.

It contains many implicit orderings as well as well as methods to construct new orderings.

Attributes

Companion
trait
Source
Ordering.scala
Supertypes
Self type
traitPartialOrdering[T] extendsEquiv[T]

A trait for representing partial orderings.

A trait for representing partial orderings. It is important to distinguish between a type that has a partial order and a representation of partial ordering on some type. This trait is for representing the latter.

Apartial ordering is a binary relation on a typeT, exposed as thelteq method of this trait. This relation must be:

- reflexive:lteq(x, x) ==true, for anyx of typeT. - anti-symmetric: iflteq(x, y) ==true andlteq(y, x) ==true thenequiv(x, y) ==true, for anyx andy of typeT. - transitive: iflteq(x, y) ==true andlteq(y, z) ==true thenlteq(x, z) ==true, for anyx,y, andz of typeT.

Additionally, a partial ordering induces anequivalence relation on a typeT:x andy of typeT are equivalent if and only iflteq(x, y) && lteq(y, x) ==true. This equivalence relation is exposed as theequiv method, inherited from theEquiv trait.

Attributes

Companion
object
Source
PartialOrdering.scala
Supertypes
traitEquiv[T]
classObject
traitMatchable
classAny
Known subtypes
traitOrdering[T]
traitNumeric[T]
traitFractional[T]
traitIntegral[T]
objectBigDecimal
objectBigInt
objectBoolean
objectByte
traitCachedReverse[T]
objectInt
objectChar
objectLong
objectShort
objectString
objectSymbol
objectUnit
Show all
Self type

Attributes

Companion
trait
Source
PartialOrdering.scala
Supertypes
classObject
traitMatchable
classAny
Self type

A class for partially ordered data.

A class for partially ordered data.

Attributes

Source
PartiallyOrdered.scala
Supertypes
classAny

Conversions which present a consistent conversion interface across all the numeric types, suitable for use in value classes.

Conversions which present a consistent conversion interface across all the numeric types, suitable for use in value classes.

Attributes

Source
ScalaNumericConversions.scala
Supertypes
classAny
Known subtypes

A slightly more specific conversion trait for classes which extend ScalaNumber (which excludes value classes.)

A slightly more specific conversion trait for classes which extend ScalaNumber (which excludes value classes.)

Attributes

Source
ScalaNumericConversions.scala
Supertypes
classScalaNumber
classNumber
classObject
traitMatchable
classAny
Show all
Known subtypes
classBigInt
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp