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, opsionalKondisi 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
**kwargsUntuk argumen khusus kata kunci lainnya, lihat
Pengembalian . y ndarrayLogaritma 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.63973851319556061
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.63973851319556063 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556064
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.63973851319556066 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556067Syntax : 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.6Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556069Syntax : 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.0Syntax : 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.63973851319556066 Logarithm base 10 of 14 is : 1.1461280356782381Syntax : 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.4Syntax : 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.8073549220576043. 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.63973851319556061
Logarithm base 10 of 14 is : 1.146128035678238_5
Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556063 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556064
Logarithm base 10 of 14 is : 1.146128035678238_8
Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556066 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556067Syntax : 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.1Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556069Syntax : 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.0Syntax : 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.63973851319556066 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.6Syntax : 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.4Syntax : 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.1461280356782383. 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.63973851319556061
Logarithm(1+a) value of 14 is : 2.70805020110221_0
Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556063 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556064
Logarithm(1+a) value of 14 is : 2.70805020110221_3
Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556066 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556067Logarithm(1+a) value of 14 is : 2.708050201102216Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556069Syntax : 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.0Syntax : 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.63973851319556066 log(a) value of -14 is : 1Syntax : 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.4Syntax : 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.70805020110221Pengecualian
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.63973851319556063 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556064
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.63973851319556066 Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556067Traceback (most recent call last): File "/home/8a74e9d7e5adfdb902ab15712cbaafe2.py", line 9, in print (math.log(-14)) ValueError: math domain error2Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.63973851319556069Syntax : 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.0Syntax : 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.63973851319556066 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.3Traceback (most recent call last): File "/home/8a74e9d7e5adfdb902ab15712cbaafe2.py", line 9, in print (math.log(-14)) ValueError: math domain error8Syntax : 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.4Syntax : 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 errorAplikasi praktis
Salah satu penerapan fungsi log10() adalah digunakan untuk menghitung no. dari digit angka. Kode di bawah ini menggambarkan hal yang sama