RE: [SDK]Double to long in c++ ?

Date : Thu, 7 Jun 2007 18:59:05 +0100
To : <XSI(at)Softimage.COM>
From : "Kim Aldis" <XSI(at)kim-aldis.co.uk>
Subject : RE: [SDK]Double to long in c++ ?

You sure?

 

(long)t*3*nPoints casts t then multiplies by 3 then nbPoints.

(long)(t*3*nPoints) is ambiguous because the compiler has to work out what you want – t int or 3/nPoints float).

 

I’d take the former, it’s completely un-ambiguous.

 

From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf Of Mathieu Leclaire
Sent: 07 June 2007 18:49
To: XSI(at)Softimage.COM
Subject: RE: [SDK]Double to long in c++ ?

 

Wow, my emails really don’t seam to pass through. I’ve been trying to send this reply since yesterday. Let me try one more time…

 

Here’s what I believe is happening:

 

You have a space between (long) and t. So I believe doing long id = (long) t * 3 * nbPoints; is the equivalent of doing long id = (long)(t * 3 * nbPoints); I think this way it converts the result of the entire equation so if t = 1.99, then you get something like 23.88 and then cast it to a long so it'll only keep the 23. But if you remove the space it’ll do the equivalent of long id = (long)(t) * 3 * nbPoints; but if you have 1.99 and cast it to a long, it'll only keep the value 1 which will screw up the result even more. So you can use round(t) and that'll convert a 1.5 to a 2 and a 1.49 to 1. 

 

But I still find it weird that when you log it, it only shows the value 2 even if the real value would be 1.99. But try those and let me know if it helps.

 

I think round is in Math.h so you might need to include that library.

 

 

Mathieu Leclaire

R&D Programmer

Hybride Technologies

 

"Yesterday is history, tomorrow is a mystery, today is a gift and that is why it's called the present"

 

-----Original Message-----
From: Luc-Eric Rousseau [mailto:lucer(at)Softimage.COM]
Sent: Thursday, June 07, 2007 1:14 PM
To: XSI(at)Softimage.COM
Subject: RE: [SDK]Double to long in c++ ?

 

I perfer to use 0.25, because if you're rendering in fields in XSI the time will be 1.0, 1.5, 2.0,

2.5.  but in fact any small espilon number is fine

 


From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf Of Kim Aldis
Posted At: Thursday, June 07, 2007 1:07 PM
Posted To: xsi
Conversation: [SDK]Double to long in c++ ?
Subject: RE: [SDK]Double to long in c++ ?

Shouldn’t that be 0.5, not 0.25?

 

From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf Of Luc-Eric Rousseau
Sent: 07 June 2007 02:10
To: XSI(at)Softimage.COM
Subject: RE: [SDK]Double to long in c++ ?

 

these additionnal casts are not necessary because the entire _expression_ is being promoted to 'double' already, and you do need my +0.25 epsilon to control the truncation that hapens with the cast to 'long'

 


From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf Of Wessam Al-Bahnasi
Posted At: Thursday, June 07, 2007 12:03 AM
Posted To: xsi
Conversation: [SDK]Double to long in c++ ?
Subject: RE: [SDK]Double to long in c++ ?

Or better yet:

 

double t = (double)T + (double)oOffset;
long id = (long)(t * 3.0 * (double)nbPoints);

 

Wessam Bahnassi

Microsoft DirectX MVP,

Programmer

Electronic Arts

--

'Talk is cheap because supply exceeds demand'

 

From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf Of Luc-Eric Rousseau
Sent: Wednesday, June 06, 2007 3:31 PM
To: XSI(at)Softimage.COM
Subject: RE: [SDK]Double to long in c++ ?

 

I don't really know the answer to your question, so I appologize if this is wrong, but the cast operator has higher precedence than the multiply. so it's going to take that 't', cast it to a long, and do the rest in longs.

I think you wanted to have another set of parenthesis like (long)(t*3*nbPoints)

 

in any case,

casting a double to long just like that is not a good idea, you should add an epsilon to compensate for any numerical innacuracies.

For example  :  long id = (long)(t*3*nbPoints + 0.25);

 


From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf Of Ahmidou Lyazidi
Posted At: Wednesday, June 06, 2007 3:20 PM
Posted To: xsi
Conversation: [SDK]Double to long in c++ ?
Subject: [SDK]Double to long in c++ ?

Hi
In an Operator,we are trying to convert the datas passed by OperatorContex.GetTime to a long.The thing that is strange is when I log the value
it seems good, but when I mutiply it with a fixed value it seems to round it and give me 2 equal values for 2 differant frame
here is the code and the log:

long oOffset = oOp.GetParameterValue(L"TimeOffset");

CTime T = ctxt.GetTime();
Application().LogMessage(L"Current operator Time " + T.GetAsText());

double t = double(T) + oOffset;
long id = (long) t * 3 * nbPoints;


Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.