8.13. Number (নাম্বার) এবং Math (ম্যাথ)
Number (নাম্বার)
জাভাস্ক্রিপ্টে Number হচ্ছে একটি primitive wrapper object অর্থাৎ এর অন্তর্গত কোন object বা method নেই। দ্বিভিন্ন ধরনের Number প্রকাশ ও ম্যানুপুলেট করতে Number ব্যবহার করা হয়। Number() constructor টি বিভিন্ন ধরনের constants and methods ধারণ করে যাদের দ্বারা number নিয়ে কাজ করা হয়। অন্য type এর value কে এই Number() constructor দ্বারা number এ পরিবর্তন করা সম্ভব হতে পারে!
123 // one-hundred twenty-three 123.0 // same 123 === 123.0 // true
Number এর বেশ কিছু properties রয়েছে। যেমন –
Number.MAX_SAFE_INTEGER দ্বারা জাভাস্ক্রিপ্টে maximum safe integer প্রকাশ করা হয় যার ভ্যালু হচ্ছে (2^53 – 1).
Number এর বেশ কিছু properties রয়েছে। যেমন –
Number.MIN_SAFE_INTEGER দ্বারা জাভাস্ক্রিপ্টে minimum safe integer প্রকাশ করা হয় যার ভ্যালু হচ্ছে (-(2^53 – 1)).
Number.MIN_VALUE _INTEGER দ্বারা জাভাস্ক্রিপ্টে শুন্যের কাছাকাছি সর্বনিম্ন সংখ্যা কে প্রকাশ করা হয় যার ভ্যালু হচ্ছে 5e-324.
Number.NaN : Not a Number কে প্রকাশ করে।
Number.NEGATIVE_INFINITY : একটি special value যা negative infinity কে প্রকাশ করে।
Number.POSITIVE_INFINITY : একটি special value যা positive infinity কে প্রকাশ করে।
এছাড়াও জাভস্ক্রিপ্টে Number এর বেশকিছু instances ও method ও রয়েছে।
Math (ম্যাথ)
জাভাস্ক্রিপ্টে Mathematical কাজ সম্পাদন করার জন্য Math object টি ব্যবহার করা হয়। এর কিছু properties & methods রয়েছে।
Properties of Math :
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
উদাহরন
- উদাহরণ- ১
Number("123"); // returns the number 123 Number("123") === 123; // true
- উদাহরণ- ২
Number("unicorn") // NaN Number(undefined) // NaN
- উদাহরণ- ৩
console.log(Math.round(4.4)); //4 console.log(Math.round(4.5)); // 5 console.log(Math.round(4.6)); //5
- উদাহরণ- ৪
console.log(Math.ceil(4.4)); //5 console.log(Math.ceil(4.5)); // 5 console.log(Math.ceil(4.6)); //5
- উদাহরণ- ৫
console.log(Math.floor(4.4)); //4 console.log(Math.floor(4.5)); //4 console.log(Math.floor(4.6)); //4
এসো নিজে করি
console.log(Math.trunc(4.9)); console.log(Math.trunc(4.7)); console.log(Math.trunc(4.4)); console.log(Math.trunc(4.2)); console.log(Math.trunc(-4.2));
- উল্লিখিত কোডটির আউটপুটগুলো বের কর।
console.log(Math.sign(-4)); console.log(Math.sign(0)); console.log(Math.sign(4));
- উল্লিখিত কোডটির আউটপুটগুলো বের কর।