|
 |
"Bald Eagle" <cre### [at] netscape net> wrote:
>
> But why don't you just write a macro that takes your number as an argument, and
> determines if its (abs val) is over 10 or under 1.
>
> Then you can just run a loop by multiplying/dividing by 10 until you're in that
> range, and then your exponent is the (signed) loop value.
>
Well, duh! That was an *excellent* suggestion. Forget my clumsy string-code
version; this one is certainly more compact and easy to grasp. It works with all
float values, as far as I know from many tests. I have included the code itself
and as a macro. The output is again as a #debug message.
I did have to add a slight kludge for an input value of exactly zero; it's
scientific notation initially ended up as 0.00000e-1, which is certainly not
correct. I'm not sure why that occurred in my code; it might be a subtle
'under the hood' error(?) Anyway, zero doesn't really need such notation, from
what I've read; so I eliminated its e exponent altogether. Problem solved! ;-)
---
definition of 'kludge' from Wikipedia:
"A kludge or kluge is a workaround or makeshift solution that is
clumsy, inelegant, inefficient...Its only benefit is that it
rapidly solves an important problem using available resources."
---
The code:
#local VAL = -.00067;
#local SIGNIFICANT_DIGITS =3;
#local ABS_VAL = abs(VAL);
// counters
#local C1 = 0;
#local C2 = 0;
#local C3 = 0;
#local C4 = 0;
#if(ABS_VAL <= 1.0 & ABS_VAL !=0.0 & ABS_VAL != -0.0)
#while(C4 <= 25) // arbitrary large value
#if(ABS_VAL < pow(10,C3))
#local C3 = C3-1;
#local C4 = C4+1;
#else
#local C4 = 1000; // to stop the while loop
#end
#end
#local VAL_1 = VAL/pow(10,(C3));
#debug concat("\n","VAL_1 as SCI NOTE = ",
str(VAL_1,0,SIGNIFICANT_DIGITS),"e",str(C3,0,0),"\n")
#else // ABS_VAL > 1.0)
#while(C2 <= 25) // arbitrary large value
#if(ABS_VAL > pow(10,C1))
#local C1 = C1+1;
#local C2 = C2+1;
#else
#local C2 = 1000; // to stop the while loop
#end
#end
#local VAL_2 = VAL/pow(10,(C1-1));
#debug concat("\n","VAL_2 as SCI NOTE = ",
str(VAL_2,0,SIGNIFICANT_DIGITS),
#if(VAL_2 = 0) // a bandaid kludge for a problem when VAL = 0.
"\n")
#else
"e",str(C1-1,0,0),"\n")
#end
#end
//-------------------
//as a MACRO:
(Note: SD -- significant digits desired in the output)
#macro SCI_NOTA_CONVERS(VAL,SD)
#local ABS_VAL = abs(VAL);
#local C1 = 0;
#local C2 = 0;
#local C3 = 0;
#local C4 = 0;
#if(ABS_VAL <= 1.0 & ABS_VAL !=0.0 & ABS_VAL != -0.0)
#while(C4 <= 25)
#if(ABS_VAL < pow(10,C3))
#local C3 = C3-1;
#local C4 = C4+1;
#else
#local C4 = 1000;
#end
#end
#local VAL_1 = VAL/pow(10,(C3));
#debug concat("\n","VAL_1 as SCI NOTE = ",
str(VAL_1,0,SIGNIFICANT_DIGITS),"e",str(C3,0,0),"\n")
#else // ABS_VAL > 1.0)
#while(C2 <= 25)
#if(ABS_VAL > pow(10,C1))
#local C1 = C1+1;
#local C2 = C2+1;
#else
#local C2 = 1000;
#end
#end
#local VAL_2 = VAL/pow(10,(C1-1));
#debug concat("\n","VAL_2 as SCI NOTE = ",
str(VAL_2,0,SIGNIFICANT_DIGITS),
#if(VAL_2 = 0)
"\n")
#else
"e",str(C1-1,0,0),"\n")
#end
#end
#end // of macro
// using it:
SCI_NOTA_CONVERS(467.00953,8)
Post a reply to this message
|
 |