round.gwyn.html

来自「this is a mirrored site c-faq. thought 」· HTML 代码 · 共 52 行

HTML
52
字号
<html><!-- Mirrored from c-faq.com/fp/round.gwyn.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:03:00 GMT --><head><title></title></head><body><p>From: Douglas A. Gwyn<br>Subject: Re: precision of a double ?<br>Date: 18 Aug 2000 00:00:00 GMT<br>Message-ID: &lt;clcm-20000818-0004@plethora.net&gt;<br>Newsgroups: comp.lang.c.moderated<p>"rhim.m" wrote:<br>&gt; I'm looking for a function such as<br>&gt; <TT>void setprecision(int precision , double in, double *out )</TT> .<br>&gt; this function can set the precision  of a double  (numbre of decimals ).<br>&gt; for exemple , a double "35.425" and a precision "1" are the inputs ;<br>&gt; the output is "35.4".<p>This is usually called ``rounding''.  It's really not very hard:<pre>        #include &lt;assert.h&gt;        #include &lt;math.h&gt;        double round2mod(double x, double module) {                if (module == 0) return x;                assert(module &gt; 0);                if (x &lt; 0) return -round2mod(-x, module);                return x + module/2 - fmod(x + module/2, module);        }        void setprecision(int precision, double in, double *out) {                *out = round2mod(in, pow(10, -precision));        }</pre>-- <br>comp.lang.c.moderated - moderation address: clcm@plethora.net</body><!-- Mirrored from c-faq.com/fp/round.gwyn.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:03:00 GMT --></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?