Math के Functions
Bash का अपना math सिर्फ कुछ buttons वाले एक basic calculator जैसा है। Square roots जैसी fancier tricks के लिए, यह help के लिए एक बड़े calculator को call करता है।
In this page:
Syntax
result=$(( base ** exponent ))
result=$(echo "function(number)" | bc -l)
Math Functions
Bash में खुद $(( )) में integer exponentiation के लिए ** के अलावा square root या power जैसा कोई built-in function नहीं है।
ज़्यादा advanced किसी भी चीज़ के लिए, scripts bc -l (जो sqrt(), sine के लिए s(), आदि support करता है) या awk को shell out करती हैं। यह जानना कि कब एक external tool को delegate करना है, effective Bash लिखने का हिस्सा है।
Note:
$(( 2 ** 10 )) बिना bc की ज़रूरत के directly integer powers compute करता है।उदाहरण: Math Functions
#!/bin/bash
echo "2^10 = $((2 ** 10))"
sqrt_result=$(echo "sqrt(16)" | bc -l)
echo "sqrt(16) = $sqrt_result"
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- Bash से
sqrt()याsin()देने की उम्मीद करना, जब integer arithmetic में कोई नहीं है;bc -lउपयोग करें। - यह भूल जाना कि
RANDOM0 से 32767 तक एक integer देता है। $(( 7 / 2 ))से round होने की उम्मीद करना, जब यह truncate करता है।
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: