2˘:

#!/usr/bin/env perl use strict; use warnings; use feature qw(say); use Math::Round; say 8.95 * 100; printf("%.15f\n", 8.95 * 100); printf("%f\n", 8.95 * 100); printf("%d\n", 8.95 * 100); printf("%d\n", round(8.95 * 100)); say int(8.95 * 100); say int(round(8.95 * 100)); __END__
karl@rantanplan:~/src/cpp/acme$ ./meditation.pl 895 894.999999999999886 895.000000 894 895 894 895
#include <iomanip> #include <iostream> #include <cmath> using std::cout; using std::round; int main() { double a = 8.95; double b = 100; double r = a * b; cout.precision(18); cout << r << " - precision 18\n"; cout << int(r) << " - int \n"; cout << round(r) << " - round\n"; cout.precision(6); cout << r << " - precision 6\n"; cout << "Supernatural!\n"; return 1; }
karl@rantanplan:~/src/cpp/acme$ clang++-18 -std=c++23 meditation.cpp - +o meditation karl@rantanplan:~/src/cpp/acme$ ./meditation 894.999999999999886 - precision 18 894 - int 895 - round 895 - precision 6 Supernatural!

perlnumber


In reply to Re: Behaviour of int() unexpected by karlgoethebier
in thread Behaviour of int() unexpected by ceade1000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.