#include <iostream>
#include <cmath>
int main() {
double n1 = 1.75;
double n2 = -1.75;
std::cout << "Fract n1 = " << (n1 - (int)n1)
<< ", n1 = " << (n2 - (int)n2) << std::endl;
std::cout << "Fract n1 = " << (n1 - floor(n1))
<< ", n2 = " << (n2 - floor(n2))<< std::endl;
std::cout << "Fract n1 = " << fmod(n1,1.0)
<< ", n2 = " << Bfmod(n2,1.0) << std::endl;
return 0;
}
will output:
Fract n1 = 0.75, n1 = -0.75
Fract n1 = 0.75, n2 = 0.25
Fract n1 = 0.75, n2 = -0.75
Note that the (n - (int)n) alternative is only valid within the numeric
range of an int...
/Per W
On Sun, 28 Sep 2003, Tim Schumacher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi List!
>
> I have a problem with getting the floatpart from a float. My code to
> do this
> looks so atm:
>
> double number;
> number = number - (int)number;
>
> but with this usage I always get 0 for number. Does someone
> know how to
> get the floatpart and can tell me?
>
> so long
>
> Tim
>
> - --
> WE ARE TEH BORG!
> RESSISTANCE IS FUTILE!
> http://we-are-teh-b.org/~tim/borg.key
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.0.6 (MingW32) - WinPT 0.5.6
>
> iD8DBQE/dugtsMXChvH2qP0RArldAKCyN/oDTMOkppsAneVfnc2laQaRSACgmfHN
> yEoHOoLKPP+n/OzQ6Dsy05A=
> =aDJe
> -----END PGP SIGNATURE-----
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|