Cara masuk2 dengan python

Fungsi matematika log2 Python digunakan untuk menghitung nilai logaritmik dari sejumlah basis 2. Pada bagian ini, kita membahas cara menggunakan fungsi log2 matematika dalam bahasa pemrograman ini dengan sebuah contoh

Sintaks dari Fungsi log2 adalah

math.log2(number);
  • Jika argumen angka adalah angka positif, fungsi mengembalikan output
  • Jika argumen angka adalah angka Negatif atau Nol, ia mengembalikan ValueError
  • Dan jika itu bukan angka, itu mengembalikan TypeError

Contoh Fungsi Python log2

Fungsi Python log2 menghitung nilai logaritmik dari sejumlah basis 2. Dalam contoh ini, Kami akan memeriksa nilai logaritmik basis 2 dengan tipe data yang berbeda dan menampilkan hasilnya

TIP. Fungsi log2 matematika lebih akurat daripada matematika. log(x, 2). Silakan merujuk ke artikel logaritma untuk memahami fungsi log

Sebuah lokasi di mana hasilnya disimpan. Jika disediakan, itu harus memiliki bentuk yang dipancarkan oleh input. Jika tidak disediakan atau Tidak Ada, array yang baru dialokasikan dikembalikan. Tuple (mungkin hanya sebagai argumen kata kunci) harus memiliki panjang yang sama dengan jumlah keluaran

di mana array_like, opsional

Kondisi ini disiarkan melalui input. Di lokasi dengan kondisi True, larik keluar akan disetel ke hasil ufunc. Di tempat lain, out array akan mempertahankan nilai aslinya. Perhatikan bahwa jika array keluar yang tidak diinisialisasi dibuat melalui default out=None, lokasi di dalamnya dengan kondisi False akan tetap tidak diinisialisasi

**kwargs

Untuk argumen khusus kata kunci lainnya, lihat

Pengembalian . y ndarray

Logaritma basis-2 dari x. Ini adalah skalar jika x adalah skalar

Lihat juga

, , ,

Catatan

Baru di versi 1. 3. 0

Logaritma adalah fungsi multinilai. untuk setiap x ada jumlah z yang tak terhingga sehingga 2**z = x. Konvensinya adalah mengembalikan z yang bagian imajinernya terletak di (-pi, pi]

Untuk tipe data input bernilai nyata, selalu mengembalikan output nyata. Untuk setiap nilai yang tidak dapat dinyatakan sebagai bilangan real atau tak terhingga, menghasilkan nan dan menyetel bendera kesalahan titik mengambang yang tidak valid

Untuk input bernilai kompleks, adalah fungsi analitik kompleks yang memiliki potongan cabang [-inf, 0] dan kontinu dari atasnya. menangani floating-point negative zero sebagai bilangan negatif sangat kecil, sesuai dengan standar C99

Dalam kasus di mana input memiliki bagian real negatif dan bagian kompleks negatif yang sangat kecil (mendekati 0), hasilnya sangat dekat dengan -pi sehingga dievaluasi dengan tepat -pi

2. log2(a). Fungsi ini digunakan untuk menghitung basis logaritma 2 dari a. Menampilkan hasil yang lebih akurat daripada log(a,2)

Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
_

Python3




Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
1

Syntax :
math.log10(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 10 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
0

 

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
3
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
4

 

Syntax :
math.log10(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 10 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
_3

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
7
Syntax :
math.log10(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 10 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
6
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
9
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
0
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
1

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
Logarithm base 10 of 14 is : 1.146128035678238
1
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
4
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
5

Keluaran.  

Logarithm base 2 of 14 is : 3.807354922057604

3. log10(a). Fungsi ini digunakan untuk menghitung logaritma basis 10 dari a. Menampilkan hasil yang lebih akurat daripada log(a,10)

Syntax :
math.log10(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 10 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.

Python3




Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
1

Logarithm base 10 of 14 is : 1.146128035678238
_5

 

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
3
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
4

 

Logarithm base 10 of 14 is : 1.146128035678238
_8

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
7
Syntax :
math.log1p(a)
Parameters : 
a : The numeric value
Return Value : 
Returns log(1+a)
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
1
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
9
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
0
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
1

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
Syntax :
math.log1p(a)
Parameters : 
a : The numeric value
Return Value : 
Returns log(1+a)
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
6
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
4
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
5

Keluaran.  

Logarithm base 10 of 14 is : 1.146128035678238

3. log1p(a). Fungsi ini digunakan untuk menghitung logaritma (1+a).  

Syntax :
math.log1p(a)
Parameters : 
a : The numeric value
Return Value : 
Returns log(1+a)
Exceptions : 
Raises ValueError if a negative no. is passed as argument.

Python3




Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
1

Logarithm(1+a) value of 14 is : 2.70805020110221
_0

 

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
3
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
4

 

Logarithm(1+a) value of 14 is : 2.70805020110221
_3

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
7
Logarithm(1+a) value of 14 is : 2.70805020110221
6
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
9
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
0
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
1

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
log(a) value of -14 is : 
1
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
4
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
5

Keluaran.  

Logarithm(1+a) value of 14 is : 2.70805020110221

Pengecualian

1. ValueError. Fungsi ini mengembalikan kesalahan nilai jika angka negatif.  

Python3




log(a) value of -14 is : 
_4

log(a) value of -14 is : 
_5

 

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
3
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
4

 

log(a) value of -14 is : 
_8

log(a) value of -14 is : 
_9

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
7
Traceback (most recent call last):
  File "/home/8a74e9d7e5adfdb902ab15712cbaafe2.py", line 9, in 
    print (math.log(-14))
ValueError: math domain error
2
Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
9
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
0
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
1

Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
6
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
3
Traceback (most recent call last):
  File "/home/8a74e9d7e5adfdb902ab15712cbaafe2.py", line 9, in 
    print (math.log(-14))
ValueError: math domain error
8
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
4
Syntax :
math.log2(a)
Parameters : 
a : The numeric value
Return Value : 
Returns logarithm base 2 of a
Exceptions : 
Raises ValueError if a negative no. is passed as argument.
5

Keluaran.  

log(a) value of -14 is : 

Kesalahan waktu proses.  

Traceback (most recent call last):
  File "/home/8a74e9d7e5adfdb902ab15712cbaafe2.py", line 9, in 
    print (math.log(-14))
ValueError: math domain error

Aplikasi praktis

Salah satu penerapan fungsi log10() adalah digunakan untuk menghitung no. dari digit angka. Kode di bawah ini menggambarkan hal yang sama

Bagaimana cara kode log2?

Program C untuk memahami cara kerja fungsi log2. .
#include .
#include .
int utama()
ganda x = 4. 2, hasil;
hasil = log2(x);
printf("log2(%lf) = %lf",x,hasil);
kembali 0;

Bagaimana cara login basis 2 dengan Python numpy?

Untuk mengembalikan logaritma basis 2 dari array input, gunakan numpy. log2() method dengan Python Numpy Metode mengembalikan logaritma Base-2 dari x. Ini adalah skalar jika x adalah skalar. Parameter pertama, x adalah nilai input, seperti array.