Thread overview
floats: ceil & floor
Aug 03, 2002
Robert M. Münch
Aug 03, 2002
Burton Radons
Aug 03, 2002
Pavel Minayev
Aug 03, 2002
Dario
Aug 03, 2002
Pavel Minayev
Aug 04, 2002
Robert M. Münch
Aug 04, 2002
Robert M. Munch
Aug 04, 2002
Pavel Minayev
August 03, 2002
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



August 03, 2002
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.

August 03, 2002
On Sat, 03 Aug 2002 05:09:27 -0700 Burton Radons <loth@users.sourceforge.net> wrote:

> 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.

Yes, exactly. The return type for all math functions is either int or extended (two overloaded versions exist, typically).

August 03, 2002
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).


August 03, 2002
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).
August 04, 2002
"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



August 04, 2002
"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


August 04, 2002
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.