Extracting the decimal

Started by
6 comments, last by JohnBolton 18 years, 2 months ago
Is there a function to extract only the decmial from an float value? I need to run it in a pixel shader, so it has to be relativily fast. Unfortunalty I have not needed this in the past, so I have no idea what it would be. I do not know the value passed in, so it can be 0.1 - 100000000.99 [Edited by - Valor Knight on January 26, 2006 1:11:26 AM]
There is no life without honor
Advertisement
#include <math.h>double decimal, integer, some_number;decimal = modf(some_number,&integer);
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
double number=35.049;double decimal=number-(float)((int)number);//decimal= 0.049


Would that work?

edit: just looked up the modf function as described above and it seems like a better solution.
It needs to run in a pixel shader so #include <math.h> won't work, and I'm not sure the casts will work.

Quote:
frac - HLSL frac(x) Returns the fractional part f of x, such that f is a value greater than or equal to 0, and less than 1.


taken directly from the DirectX SDK, in the "HLSL Intrinsic Functions" part.
Try

(((decimal * 10) % 10) / 10)

should give you the decimal only.
Imnot sure if % operator is available in shaders, but im sure that there is a function equivilant.
^^ in fact, it seems I didn't understand the question either ^^
if % doesn't work with HLSL, then :

floor(frac(i * 0.1f) * 10.0f);

should retrieve the first decimal (I'm still not sure I did understand what your want : given 123.456, you want the function to return 3 ?)
Quote:Original post by paic
should retrieve the first decimal (I'm still not sure I did understand what your want : given 123.456, you want the function to return 3 ?)

no, I need it to return 0.456

all I needed to do was: decimal - floor(decimal) to get the decimal.
Thanks

[Edited by - Valor Knight on January 26, 2006 1:34:08 PM]
There is no life without honor
Quote:Original post by Valor Knight
Quote:Original post by paic
should retrieve the first decimal (I'm still not sure I did understand what your want : given 123.456, you want the function to return 3 ?)

no, I need it to return 0.456

all I needed to do was: decimal - floor(decimal) to get the decimal.
Thanks


As paic suggested above, frac is the right thing to use.

BTW, the confusion was due to your terminology. "decimal" is a numbering system. You statement above makes as much sense as "all I needed to do was: binary - floor(binary) to get the binary"
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement