POSIX का उपयोग
POSIX module कई standard C-style math और time functions लाता है।
In this page:
POSIX
POSIX नीचे round करने और ऊपर round करने के लिए floor और ceil देता है, floating-point remainders के लिए fmod, और dates और times format करने के लिए strftime। यह INT_MAX और DBL_MAX जैसे constants भी define करता है। Default से सब कुछ import करना noisy है, इसलिए सिर्फ उन names के लिए पूछें जो आपको चाहिए।
Note:
Perl का built-in int zero की ओर truncate करता है, जबकि POSIX floor हमेशा negative infinity की ओर round करता है।
उदाहरण: POSIX
use strict;
use warnings;
use POSIX qw(floor ceil fmod strftime INT_MAX);
print "floor(3.7)=", floor(3.7), " ceil(3.2)=", ceil(3.2), "\n";
print "floor(-3.7)=", floor(-3.7), " int(-3.7)=", int(-3.7), "\n";
print "fmod(10.5, 3)=", fmod(10.5, 3), "\n";
print "INT_MAX=", INT_MAX, "\n";
print "epoch start: ", strftime("%Y-%m-%d %H:%M:%S", gmtime(0)), "\n";
print "a fixed moment: ", strftime("%A, %d %B %Y", gmtime(86400 * 365)), "\n";
printf "rounded: %d\n", floor(2.5 + 0.5);
# Output:
# floor(3.7)=3 ceil(3.2)=4
# floor(-3.7)=-4 int(-3.7)=-3
# fmod(10.5, 3)=1.5
# INT_MAX=2147483647
# epoch start: 1970-01-01 00:00:00
# a fixed moment: Friday, 01 January 1971
# rounded: 3
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- सारा POSIX import करना और built-in names clobber करना
- यह उम्मीद करना कि int negative numbers नीचे round करेगा
- strftime को local time pass करना और अलग machines पर अलग output पाना
चैप्टर सारांश
- floor और ceil नीचे और ऊपर round करते हैं
- fmod एक floating-point remainder देता है
- strftime time values format करता है
- सिर्फ ज़रूरी names import करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: