Giriş
java.lang paketi kendiliğinden dahil edildiği için şu satıra gerek
yok.
import java.lang.Math;
Math ve StrictMath Farkı Nedir
Açıklaması
şöyle. Yani Math paket intermediate results için yüksek çözünürlükte bit kullanabilir.
Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.
By default many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.
Kaynak Kodu Okumak
Kaynak kodda StrictMath çağrısı görülse bile JVM bunu değiştirmekte serbesttir. Elimizdeki pow() metodu şöyle
olsunpublic static double pow(double a, double b) {
return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}
But the JVM is free to implement it with intrinsics or runtime calls, thus the returning result can be different from what we would expect from StrictMath.pow.
The reason?
Well, it's because OpenJDK uses intrinsics and runtime functions to implement Math.pow (and other math functions), instead of just executing the Java code. The main purpose is to take advantage of x87 instructions so that performance for the computation can be boosted. As a result, StrictMath.pow is never called from Math.pow at runtime (for the OpenJDK version that we just used, to be precise).
abs metodu
double alıp double dönen overload için şöyle
yaparız.
double nmber = ...;
double number_abs = Math.abs (number);
acos metodu
Örnek ver
atan2 metodu
Metodun içi
şöyle.
public static double atan2(double y, double x) {
return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
}
cos metodu
Şöyle
yaparız.
double latitudeRad = ...;
Math.cos(lat1Rad);
ceil metodu
Örnek
Şöyle yaparız. Burada jarSize 149 partSize = 50 olsun. Bölüm 2.xxx olacaktır. ceil() ise bunu 3'e yuvarlar.
protected int calculateTotalParts(long jarSize, int partSize) {
return (int) Math.ceil(jarSize / (double) partSize);
}
Aynı şey şöyle de olabilir. jarSize 149 partSize = 50 olsun. Sonuç yine 3 çıkar
return (jarSize + partSize - 1) / partSize;
veya şöyle yaparız
int calculateTotalParts(long jarSize, long partSize) {
long mod = jarSize % partSize;
int remainder = (mod == 0L) ? 0 : 1;
int result = (int) (jarSize / partSize);
return result + remainder;
}
exp metodu
e üzeri x'in sonucunu döner. Şöyle
yaparız.
double x = ...;
double result = Math.exp (x);
floorDiv metodu
Şöyle
yaparız.
long epochMilli = ...;
long secs = Math.floorDiv(epochMilli, 1000);
floorMod metodu
Şöyle
yaparız.
long epochMilli = ...;
long secs = Math.floorDiv(epochMilli, 1000);
int mos = (int)Math.floorMod(epochMilli, 1000);
fma metdu
İmzası şöyle. c + (a * b) sonucunu döner
public static double fma(double a, double b, double c);
Örnek
// FMA: Fused Multiply Add: c = c + (a * b)
public static float scalarFMA(float[] a, float[] b){
var c = 0.0f;
for(var i=0; i < a.length; i++){
c = Math.fma(a[i], b[i], c);
}
return c;
}hypot metodu
Şöyle
yaparız.
Math.hypot(x,y);
log10 metoduBir sayının küsurattan önce kaç basamağa sahip olduğu bulunur
Örnek
Şöyle
yaparız.
Math.log10(2);
min metodu
Şöyle
yaparız.
int hours = ...;
int hoursWorked = Math.min (8, hours);
max metodu
Şöyle
yaparız.
int hours = ...;
int hoursWorked = Math.max (8, hours);
İkiden fazla parametre vermek istersek şöyle
yaparız.
Integer maxValue = Math.max(Collections.max(data1),
Math.max(Collections.max(data2), Collections.max(data3)));
nextAfter metodu
Şöyle
yaparız. Math.ulp() pozitif sonraki sayı ile olan farkı verir. Bu fark nextAfter() ile geriye giderken ile aynı değer değildir.
double x = 1;
System.out.println(x - Math.ulp(x) == Math.nextAfter(x, Double.NEGATIVE_INFINITY));
System.out.println(x + Math.ulp(x) == Math.nextAfter(x, Double.POSITIVE_INFINITY));
Çıktı olarak şunu
alırız.
false
true
ulp metodu - Bir sonraki sayı ile farkı pozitif sayı olarak verir
Açıklaması
şöyle
Math.ulp(double) is defined to return "the positive distance between this floating-point value and the double value next larger in magnitude"
Örne
Bir sonraki sayıya geçmek için şöyle
yaparız
current + Math.ulp(current);
Örnek
Şöyle
yaparız.
double x = 1;
System.out.println(Math.ulp(x));
PI Alanı
Şöyle
yaparız.
static double getCircumference(double radius ) {
return Math.PI * 2 * radius;
}
public static double getArea(double radius) {
return Math.PI * radius * radius;
}
pow metodu
double tipinden parametreler alır ve double bir sonuç döner.
Örnek
Şöyle
yaparız.
var exp = 100 * Math.pow(2, 4);
Örnek
Şöyle
yaparız.
Math.pow(2,31);
random metodu
Açıklaması
şöyle. Math sınıfı içindeki
Random nesnesinin kullandığı için Random.nextDouble() ile aynı işi yapar.
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Şöyle
yaparız.
double randVal = Math.random();
Sayı [0,1) aralığında olduğu için şöyle yapmak hep 0 sonucunu
verir.
double randVal = (int) Math.random();
round metoduMath.round metodu yazısına taşıdım.
signum metodu
Açıklaması
şöyle. sadece
double ve
float içindir. Integer.signum() ile aynıdır.
Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
sin metodu
Şöyle
yaparız.
double latitudeRad = ...;
Math.sin(lat1Rad);
toIntExact metodu
Math Sınıfı Overflow Kontrolü Yapan Metodlar yazısına taşıdım.
toDegrees metodu
Radyan'dan dereceye çevirir.
Örnek
Şöyle
yaparız.
Math.toDegrees(...);
toRadians metodu
Dereceden radyan'a çevirir.
Örnek
Şöyle
yaparız.
double latitude = ...;
double latitudeRad = Math.toRadians(latitude);