POV-Ray : Newsgroups : povray.newusers : scientific notation : Re: scientific notation Server Time
3 Oct 2025 04:18:46 EDT (-0400)
  Re: scientific notation  
From: Kenneth
Date: 22 Sep 2025 23:20:00
Message: <web.68d20fdff4db7ec9e83955656e066e29@news.povray.org>
Here's my attempt, using string commands-- which are a bit tricky to implement;
there are a few 'hidden rules' that I had to tease out, particularly for the
strlen(...) and substr(...) functions.

The output is in the form of a #debug.

The code is NOT yet completely finished-- and is certainly not elegant-- but I
wanted to post what I have. It works for all positive and negative floats,
*except* for values between +1.0 and -1.0 -- like .078463. No fatal errors for
those, but their resulting scientific notation output is not yet in the proper
form... like 0.078463e0 instead of 7.8463e-2. I'm still working on that; it's a
question of picking the correct *location* in the array of the value-as-strings,
for placing the added decimal point in the proper place for those. (The solution
probably lies in adding more conditionals in the final #debug section, although
I haven't yet figured out where.) The code does work fine for a zero value
though.

The code needs only two entries from the user: the float value to convert, and
the number of significant digits desired in the output.

//-----------
#declare VAL = 347.19054;
#declare SIGNIFICANT_DIGITS = 5; // MUST BE AT LEAST 1, otherwise the final
// result in #debug returns the wrong e exponential value.

#declare VAL_AS_STRING = str(VAL,0,SIGNIFICANT_DIGITS)
#declare STR_LENGTH = strlen(VAL_AS_STRING);
#declare DEC_POINT = "."

#declare C = 1; // a counter
#while(C <= STR_LENGTH)
#declare SS = substr(VAL_AS_STRING,C,1)
#if(SS=DEC_POINT)
#declare COUNT_AT_DECIMAL_POINT=C; // the decimal point position has been
// found at this position, counting from the left of the value.
#declare C = 1000; // to end the while loop
#else
#declare C = C+1;
#declare COUNT_AT_DECIMAL_POINT=0; // in case there is NO decimal point.
#end // of #if
#end // of #while loop

// The following sections recreate VAL (as a string) with the original
// decimal point eliminated and a new one inserted elsewhere.
#declare TEMP_ARRAY = array[STR_LENGTH]
#declare D = 0; // a counter
#while(D < STR_LENGTH)
#declare TEMP_ARRAY[D]= substr(VAL_AS_STRING,D+1,1)
#declare D = D+1;
#end

// The output:
#debug concat(
"\n",
#for(i,0,STR_LENGTH-1)
 #if(i=COUNT_AT_DECIMAL_POINT-1)
 #declare i = i+1; // gets rid of the current decimal point by skipping it
// in the array.
 #end

TEMP_ARRAY[i],
 #if(i=0 & TEMP_ARRAY[0] != "-")
 DEC_POINT, // adds a new decimal point.
 #else
  #if(i=1 & TEMP_ARRAY[0] = "-")
  DEC_POINT, // or ditto
  #end
 #end
 #end // of #for
 "e",

 #if(TEMP_ARRAY[0] != "-" & COUNT_AT_DECIMAL_POINT !=0)
 str(COUNT_AT_DECIMAL_POINT-2,0,0)
 #else
 #end

 #if(TEMP_ARRAY[0] = "-" & COUNT_AT_DECIMAL_POINT !=0)
 str(COUNT_AT_DECIMAL_POINT-3,0,0)
 #else
 #end
"\n") // end of #debug
// END OF FILE


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.