www.digitalmars.com         C & C++   DMDScript  

D - floats: ceil & floor

reply "Robert M. Münch" <robert.muench robertmuench.de> writes:
Hi, I have:

 float x1 = (160 * sin((sin(t * 1.2) * 80))) + 256;
 float y1 = (100 * cos((sin(t)) * 180)) + 160;

 printf("x1: %f  y1: %f\n", x1, y1);
 printf("x1: %f  y1: %f\n", floor(x1), floor(y1));

And I get:
x1: 137.382156  y1: 238.485764
x1: 0.000000  y1: 0.000000

Any idea what the problem is?

PS: I used Pavels Math2 stuff.

--
Robert M. Münch
IT & Management Freelancer
Mobile: +49 (0)177 2452 802
Fax   : +49 (0)721 8408 9112
Web   : http://www.robertmuench.de
Aug 03 2002
parent reply Burton Radons <loth users.sourceforge.net> writes:
Robert M. Münch wrote:

  float x1 = (160 * sin((sin(t * 1.2) * 80))) + 256;
  float y1 = (100 * cos((sin(t)) * 180)) + 160;
 
  printf("x1: %f  y1: %f\n", x1, y1);
  printf("x1: %f  y1: %f\n", floor(x1), floor(y1));
 
 And I get:
 x1: 137.382156  y1: 238.485764
 x1: 0.000000  y1: 0.000000
 
 Any idea what the problem is?
Could it be because floor is returning extended when printf expects double? Try casting it to double. I'm in the wrong OS to check.
Aug 03 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Sat=2C 03 Aug 2002 05=3A09=3A27 -0700 Burton Radons
=3Cloth=40users=2Esourceforge=2Enet=3E 
wrote=3A

=3E Robert M=2E M=FCnch wrote=3A
=3E 
=3E=3E  float x1 =3D =28160 * sin=28=28sin=28t * 1=2E2=29 * 80=29=29=29 + 256=3B
=3E=3E  float y1 =3D =28100 * cos=28=28sin=28t=29=29 * 180=29=29 + 160=3B
=3E=3E 
=3E=3E  printf=28=22x1=3A %f  y1=3A %f=5Cn=22=2C x1=2C y1=29=3B
=3E=3E  printf=28=22x1=3A %f  y1=3A %f=5Cn=22=2C floor=28x1=29=2C
floor=28y1=29=29=3B
=3E=3E 
=3E=3E And I get=3A
=3E=3E x1=3A 137=2E382156  y1=3A 238=2E485764
=3E=3E x1=3A 0=2E000000  y1=3A 0=2E000000
=3E=3E 
=3E=3E Any idea what the problem is=3F
=3E 
=3E Could it be because floor is returning extended when printf expects 
=3E double=3F  Try casting it to double=2E  I'm in the wrong OS to check=2E

Yes=2C exactly=2E The return type for all math functions is either
int or extended =28two overloaded versions exist=2C typically=29=2E
Aug 03 2002
next sibling parent reply "Dario" <supdar yahoo.com> writes:
This demonstrates how dangerous printf() is!
Anyway, does printf() await a float or a double for a %f?

__________________

  float x1 = (160 * sin((sin(t * 1.2) * 80))) + 256;
  float y1 = (100 * cos((sin(t)) * 180)) + 160;

  printf("x1: %f  y1: %f\n", x1, y1);
  printf("x1: %f  y1: %f\n", floor(x1), floor(y1));

 And I get:
 x1: 137.382156  y1: 238.485764
 x1: 0.000000  y1: 0.000000

 Any idea what the problem is?
Could it be because floor is returning extended when printf expects double? Try casting it to double. I'm in the wrong OS to check.
Yes, exactly. The return type for all math functions is either int or extended (two overloaded versions exist, typically).
Aug 03 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Sat, 3 Aug 2002 20:48:45 +0200 "Dario" <supdar yahoo.com> wrote:

 This demonstrates how dangerous printf() is!
 Anyway, does printf() await a float or a double for a %f?
Double. If you pass float, it is converted to double automatically by the compiler, so no need to cast floats. Extended, however, must be casted (or %Lf used).
Aug 03 2002
parent "Robert M. Münch" <robert.muench robertmuench.de> writes:
"Pavel Minayev" <evilone omen.ru> schrieb im Newsbeitrag
news:CFN37471971797963 news.digitalmars.com...
 On Sat, 3 Aug 2002 20:48:45 +0200 "Dario" <supdar yahoo.com> wrote:
 Extended, however, must be casted (or %Lf used).
Casting works for me, using %Lf doesn't in this case I still get a 0.0 for the ceiled/floored value. Robert
Aug 04 2002
prev sibling parent reply "Robert M. Munch" <robert.muench robertmuench.de> writes:
"Pavel Minayev" <evilone omen.ru> schrieb im Newsbeitrag
news:CFN374717882683565 news.digitalmars.com...
On Sat, 03 Aug 2002 05:09:27 -0700 Burton Radons
<loth users.sourceforge.net>
wrote:

 Yes, exactly. The return type for all math functions is either
 int or extended (two overloaded versions exist, typically).
So I could use %d instead? I try this but IIRC I checked it and I got the same result... Robert
Aug 04 2002
parent Pavel Minayev <evilone omen.ru> writes:
On Sun, 4 Aug 2002 14:48:28 +0200 "Robert M. Munch" 
<robert.muench robertmuench.de> wrote:

 Yes, exactly. The return type for all math functions is either
 int or extended (two overloaded versions exist, typically).
So I could use %d instead? I try this but IIRC I checked it and I got the same result... Robert
No, you couldn't... the compiler can't determine which function to use based on format string. =) You get int, if you pass int as argument. You get extended if you pass extended. Some functions only have extended version - trigonometric, for example. I suggest looking at the source to see the details.
Aug 04 2002