PDA

View Full Version : getting rid of "e" in x,y,z coordinates.


artistx
01-31-2004, 06:24 PM
getting rid of "e" in x,y,z coordinates
Wondering if someone could help me out with a Format problem. I'm printing out points to a file, but I don't want the exponential printed out. I've notice this occuring with very small numbers in Max. For example, [-86.6734,6.41911e-007,16.7711] where the y component in the list has an exponent. I was hoping there was some easy command to get rid of this exponent and force the number to print out in decimal form, but I haven't found anything. I was wondering if someone could point me in the right direction. Am I missing something big here?

I'm currently using a simple loop to decide if the number is smaller than a certain amount (let's say .00001 for example) if the number is smaller than that, then print out "0" in place of the exponential number.

if MyPoint.y < .00001 do
(
MyPoint.y = 0
print MyPoint.y
)

Surely, there must be an easier way than this. Or is everyone else using a loop to convert their exponential numbers. Anyone know of a way to completely convert without losing any precision in the exponential number given different exponential values. Say a function that takes [7.24311e-008, 6.41911e-007, 12] and converts to [0.000000724311, 0.00000641911, 12]. Any help would be greatly appreciated. Thanks.

LFShade
01-31-2004, 08:18 PM
I don't think you'll be able to work with those as numbers (without the exponential notation), but since you're just going out to a file anyway you might try working with them as strings. It should be reasonably simple to write a function that converts, for instance, 7.24311e-008 to "0.0000000724311" as a string.

Of course, if you're going to be reading these values back into Max it will convert them to 0.0, so don't go to the extra trouble unless you're exporting for use elsewhere.

RH

artistx
02-01-2004, 04:49 AM
Hello LFShade, thanks for the reply. It was quite helpful. I know how to build a function that could convert
7.24311e-008 to "0.0000000724311" or any number that has an e-008 on the end of it. I'm not sure where to start when it comes to multiple different exponent numbers. Lets say you get the point set [7.243411e-008, 6.589346e-007, 3.142323]. Is there a blanket function that could address e-008 and e-007(and any other number with e^-nth power). Is there an "exponent-to- decimal" converter out there. Any rough ideas on where to start LFShade?

RealThing
02-01-2004, 09:30 AM
Here's a function but it's a bit messy (just got home from the bar). I can't promise that it will work in all cases so make sure you test this a bit. If you make any changes or bug fixes please post them. I'm sure this can be simplified a bit more than this as well.

to use just call either

point3string <point3 value>
or
floatstring <float>


fn floatstring num =

(

if num < 0.0001 or num > 999999.0 then

(

numstr = num as string

count = numstr.count



-- grab mantissa

mantissa = substring numstr 1 (count - 5)



-- grab sign from exponent append it to "1" and multiply by exponent

if (substring numstr (count - 3) 1) == "-" then

(

exponent = (-1 * ((substring numstr (count - 2) 3) as integer))

)

else

(

exponent = ((substring numstr (count - 2) 3) as integer)

)



-- adjust decimal place

strout = ""



if mantissa[1] == "-" do

( -- check to see if mantissa is negative

strout += "-"

mantissa = (substring mantissa 2 (mantissa.count - 1))

)



-- locate decimal place

decimal = findstring mantissa "."

mantissa = (replace mantissa decimal 1 "")



if exponent < 0 then

(

strout += "0."

for x in 0 to (abs(exponent) - decimal) do

(

strout += "0"

)

strout += mantissa

)

else

(

-- account for decimal places moved by removing decimal from string

mantcount = mantissa.count - (decimal - 1)



strout += (replace mantissa 2 1 "")

for x in 0 to (abs(exponent) - mantcount ) do

(

strout += "0"

)

strout += ".0"

)

return strout

)
else return num

)

fn point3string pt3
(
return ("[ "+(floatstring pt3.x) + " , " + (floatstring pt3.y) " , " (floatstring pt3.z) + " ] ")
)

artistx
02-02-2004, 01:57 AM
RealThing thank you for putting up that very helpful code. :)
If you wrote that up in a drunken stupor, you must be extremely productive when you're sober! The code looks workable to me. I get the gist of what you're saying. You addressed some things I hadn't considered, such as creating a case where the decimal"." is eleminated if necessary and using substring to help filter through the string count.
Thanks again. Your help is greatly appreciate. It's good to have people like RealThing and LFShade on the forums.

Kramsurfer
02-04-2004, 06:21 PM
Here's a more string based approch...

Only evaluated when max is going to put that "e-" in your giddy-up... not tested THAT much, but seems to work well in one of my scripts...

fn ValuetoString Val =
(
ValasStr= (Val as string)
if ( findstring ValasStr "e-" != undefined ) then
(
Reform = filterString ValasStr "e-."
ValasStr = "0."
for i=1 to ((Reform[3] as integer)-1) do
(
ValasStr = ValasStr + "0"
)
ValasStr=ValasStr + ( Reform[1] + Reform [2] )

)
ValasStr
)

enjoy

Keith Morrison
www.focus360.com

RealThing
02-04-2004, 09:00 PM
Looks good Keith. I would put a numeric test before converting all numbers to strings as this is a very slow process and should be avoided when possible.

RealThing
02-05-2004, 01:18 AM
Actually now that I think about it in this case you have to convert the number to a string anyhow. So your method wouldn't be slow.

Kramsurfer
02-05-2004, 01:47 AM
yeah... true... That's what I was thinking... ha ha

Keith

artistx
02-05-2004, 05:29 AM
Hello KramSurfer. Thank you for sharing your code. I just glanced it over, but it looks just as valid. Lol, it's funny how people space their code though. I smush my stuff together like KramSurfer. RealThing's code is spacious. I guess it's easier to read it that way though...
It's interesting to see where key decisions are made in the two programs. RealThing creates a range bracket for numbers and then makes decisions. KramSurfer converts the numbers to text and then looks for the "e-". I love seeing the variety of approaches.

Kramsurfer
02-05-2004, 05:10 PM
I actually spaced my code out to make it easier to read in the message. I'm a fan of ";"'s and multi statment lines in ()'s for then and do loops.

I like my whole script, with 400+ commands, to fit on the screen at one time.. ha ha...

Keith

CGTalk Moderation
01-17-2006, 08:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.