M
MichaelAtOz
Fri, Nov 18, 2016 1:53 AM
I needed to find appropriate imperial hardware sizes...
(needs snapshot for let, and 2016.11.11
[http://files.openscad.org/snapshots/] for echo function)
// Display numbers as fraction of inch
// as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128
// released to the public domain
// there may be a better way.
//
// Handle negative & whole numbers
function fractional(n) = (
//echo(str("f(",n,")") )
( n < 0 )
? str("-",fractional(abs(n)))
: ( n == floor(n) )
? str(n)
: ( n > 1 )
? str(floor(n)," & ",_f(n-floor(n))[4])
: _f(n)[4] // 4 - is the textual part
);
// Handle fractional part
// Returns vector
// [numerator, denominator, fraction as float, string fraction, x=depth]
function _f(n,num=1,den=2,tolerance=0.0005, maxDen=128, x=1) = (
let (f=num/den, r=n-f )
//echo(n=n,str(num,"/",den,"=", f, ",r=",r), x )
( n == (num/den) )
? [ num, den, f, r, str(num,"/",den), x ]
: ( x>32 || abs(r) <= tolerance )
? [ num, den, f, r, str("~",num,"/",den, r), x ]
: ( n < (num/den) )
? ( den>=maxDen )
? [ num, den, f, r, str("~",num,"/",den, r), x ]
:_f(n,num2-1,den2,x=x+1)
: _f(n,num+1,den,x=x+1)
);
echo(fractional(0.125));
echo(fractional(0.128));
echo(fractional(0.124));
echo("1/3:",fractional(1/3));
echo("pi:",fractional(3.1415926));
echo("36/64:",fractional(36/64));
echo("7/16:",fractional(7/16));
echo("-5/8:",fractional(-5/8));
testN=[1:2:16];
testD=[4:4:32];
for (n=testN,d=testD) {
echo(str(n,"/",d),fractional(n/d));
}
echo("99:",fractional(99));
echo("0:",fractional(0));
echo("-0:",fractional(-0));
echo("1:",fractional(1));
echo("-1:",fractional(-1));
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
I needed to find appropriate imperial hardware sizes...
(needs snapshot for let, and 2016.11.11
[http://files.openscad.org/snapshots/] for echo function)
> // Display numbers as fraction of inch
> // as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128
> // released to the public domain
> // there may be a better way.
> //
>
> // Handle negative & whole numbers
> function fractional(n) = (
> //echo(str("f(",n,")") )
> ( n < 0 )
> ? str("-",fractional(abs(n)))
> : ( n == floor(n) )
> ? str(n)
> : ( n > 1 )
> ? str(floor(n)," & ",_f(n-floor(n))[4])
> : _f(n)[4] // 4 - is the textual part
> );
>
> // Handle fractional part
> // Returns vector
> // [numerator, denominator, fraction as float, string fraction, x=depth]
> function _f(n,num=1,den=2,tolerance=0.0005, maxDen=128, x=1) = (
> let (f=num/den, r=n-f )
> //echo(n=n,str(num,"/",den,"=", f, ",r=",r), x )
> ( n == (num/den) )
> ? [ num, den, f, r, str(num,"/",den), x ]
> : ( x>32 || abs(r) <= tolerance )
> ? [ num, den, f, r, str("~",num,"/",den, r), x ]
> : ( n < (num/den) )
> ? ( den>=maxDen )
> ? [ num, den, f, r, str("~",num,"/",den, r), x ]
> :_f(n,num*2-1,den*2,x=x+1)
> : _f(n,num+1,den,x=x+1)
> );
>
>
> echo(fractional(0.125));
> echo(fractional(0.128));
> echo(fractional(0.124));
> echo("1/3:",fractional(1/3));
> echo("pi:",fractional(3.1415926));
> echo("36/64:",fractional(36/64));
> echo("7/16:",fractional(7/16));
> echo("-5/8:",fractional(-5/8));
>
> testN=[1:2:16];
> testD=[4:4:32];
> for (n=testN,d=testD) {
> echo(str(n,"/",d),fractional(n/d));
> }
>
> echo("99:",fractional(99));
> echo("0:",fractional(0));
> echo("-0:",fractional(-0));
> echo("1:",fractional(1));
> echo("-1:",fractional(-1));
-----
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
--
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
RP
Ronaldo Persiano
Fri, Nov 18, 2016 12:00 PM
echo(fractional(9/20)); // ECHO:"~58/128-0.003125" ???
echo(fractional(-11/8-1/256)); // ECHO: "-1 & ~49/128-0.00390625" ???
echo(-11/8-1/256); // ECHO: -1.37891
echo(-1 -49/128-0.00390625); // ECHO: -1.38672;
echo(-1 -49/128+0.00390625); // ECHO: -1.37891
echo(fractional(9/20)); // ECHO:"~58/128-0.003125" ???
echo(fractional(-11/8-1/256)); // ECHO: "-1 & ~49/128-0.00390625" ???
echo(-11/8-1/256); // ECHO: -1.37891
echo(-1 -49/128-0.00390625); // ECHO: -1.38672;
echo(-1 -49/128+0.00390625); // ECHO: -1.37891
M
MichaelAtOz
Fri, Nov 18, 2016 9:32 PM
echo(fractional(9/20)); // ECHO:"~58/128-0.003125" ???
echo(fractional(-11/8-1/256)); // ECHO: "-1 & ~49/128-0.00390625" ???
echo(-11/8-1/256); // ECHO: -1.37891
echo(-1 -49/128-0.00390625); // ECHO: -1.38672;
echo(-1 -49/128+0.00390625); // ECHO: -1.37891
"// as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128 "
(as in what bolt/screw/drill/o-ring etc sizes are close)
Design for my use to take an inch float and display a inch measure in
fractional format; 1 & 1/4".
The '~' is to flag a non exact match, v's 0.625 -> 5/8 exact match.
a. 9/20 (0.45) close estimate within those denominators is 58/128th
(0.453125) with a difference from that fraction of 0.003125.
0.453125-0.003125=0.45=9/20.
b. echo( -(1+49/128-0.00390625) ); // -1.37891
ie one and 49/128th
it's a representation of the absolute number, it could be done the other
way
thanks for highlighting it tho.
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19226.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Ronaldo wrote
> echo(fractional(9/20)); // ECHO:"~58/128-0.003125" ???
>
> echo(fractional(-11/8-1/256)); // ECHO: "-1 & ~49/128-0.00390625" ???
> echo(-11/8-1/256); // ECHO: -1.37891
> echo(-1 -49/128-0.00390625); // ECHO: -1.38672;
> echo(-1 -49/128+0.00390625); // ECHO: -1.37891
"// as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128 "
(as in what bolt/screw/drill/o-ring etc sizes are close)
Design for my use to take an inch float and display a inch measure in
fractional format; 1 & 1/4".
The '~' is to flag a non exact match, v's 0.625 -> 5/8 exact match.
a. 9/20 (0.45) close estimate within those denominators is 58/128th
(0.453125) with a difference from that fraction of 0.003125.
0.453125-0.003125=0.45=9/20.
b. echo( -(1+49/128-0.00390625) ); // -1.37891
ie one and 49/128th
it's a representation of the absolute number, it could be done the other
way
thanks for highlighting it tho.
-----
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
--
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19226.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
RP
Ronaldo Persiano
Fri, Nov 18, 2016 10:38 PM
My two points are:
a) echo(fractional(9/20)); // ECHO:"~58/128-0.003125" is not a fraction
reduced
form; I would expect ECHO:"~29/64-0.003125" with an odd numerator.
b) echo(fractional(-11/8-1/256)); // ECHO: "-1 & ~49/128-0.00390625"
your fractional expression is incorrect:
-1 - 49/128 - 0.00390625 != -11/8-1/256
the correct expression is:
-1 - 49/128 + 0.00390625 == -11/8-1/256
My two points are:
a) echo(fractional(9/20)); // ECHO:"~58/128-0.003125" is not a fraction
reduced
form; I would expect ECHO:"~29/64-0.003125" with an odd numerator.
b) echo(fractional(-11/8-1/256)); // ECHO: "-1 & ~49/128-0.00390625"
your fractional expression is incorrect:
-1 - 49/128 - 0.00390625 != -11/8-1/256
the correct expression is:
-1 - 49/128 + 0.00390625 == -11/8-1/256
M
MichaelAtOz
Sat, Nov 19, 2016 3:56 AM
OK fixed for negatives & fiddled with tolerance, but when it hits the
denominator limit, it is not necessarily the best tolerance, it just gives
up. "// there may be a better way."
// Display numbers as fraction of inch
// as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128
// released to the public domain
// there may be a better way.
//
// Handle negative & whole numbers
function fractional(n) = (
//echo(str("f(",n,")") )
( abs(n) == floor(abs(n)) )
? str(n)
: ( n > 1 )
? str(floor(n)," & ",_f(n-floor(n))[4] ) // [4] - is the textual part
: ( n > 0 )
? _f(n-floor(n))[4]
: ( n < -1 )
? str(ceil(n)," & ",_f((n-ceil(n)))[4] )
: str("-",_f(n)[4])
);
// Handle fractional part
// Returns vector
// [numerator, denominator, fraction as float, string fraction, x=depth]
function _f(n,num=1,den=2,tolerance=1/128/2, maxDen=128, x=1) = (
let (f=num/den, an=abs(n), r=an-f )
//echo(n=n,str(num,"/",den,"=", f, ",r=",r), x )
( an == f )
? //echo("match")
[ num, den, f, r, str(num,"/",den), x ]
: ( x>32 || abs(r) <= tolerance )
? //echo("x or tol",x=x,r=r)
[ num, den, f, r, str("~",num,"/",den, (r>0)?"+":"",r ), x ]
: ( an < f )
? ( den >= maxDen )
? //echo("MaxDen")
[ num, den, f, r, str("~",num,"/",den, (r>0)?"+":"",r ), x ]
: _f(n,num2-1,den2,x=x+1)
: _f(n,num+1,den,x=x+1)
);
echo(fractional(0.125));
echo(fractional(0.128));
echo(fractional(0.124));
echo("1/3:",fractional(1/3));
echo("pi:",fractional(3.1415926));
echo("-2:",fractional(-2));
echo("-1.25:",fractional(-1.25));
echo("-1/8:",fractional(-1/8));
echo("1/128:",fractional(1/128));
echo("1/127:",fractional(1/127));
echo("9/20:",fractional(9/20));
echo("-11/8-1/256",fractional(-11/8-1/256));
echo("-1-3/8-1/256:",fractional(-1-3/8-1/256));
echo(49/128,-49/128); // +-0.382812
echo( -(1+49/128)); // -1.38281
echo( -(1+49/128-0.00390625) ); // -1.37891
testN=[1:2:16];
testD=[4:4:32];
for (n=testN,d=testD) {
echo(str(n,"/",d),fractional(n/d));
}
echo("99:",fractional(99));
echo("0:",fractional(0));
echo("-0:",fractional(-0));
echo("1:",fractional(1));
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19238.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OK fixed for negatives & fiddled with tolerance, but when it hits the
denominator limit, it is not necessarily the best tolerance, it just gives
up. "// there may be a better way."
> // Display numbers as fraction of inch
> // as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128
> // released to the public domain
> // there may be a better way.
> //
>
> // Handle negative & whole numbers
> function fractional(n) = (
> //echo(str("f(",n,")") )
> ( abs(n) == floor(abs(n)) )
> ? str(n)
> : ( n > 1 )
> ? str(floor(n)," & ",_f(n-floor(n))[4] ) // [4] - is the textual part
> : ( n > 0 )
> ? _f(n-floor(n))[4]
> : ( n < -1 )
> ? str(ceil(n)," & ",_f((n-ceil(n)))[4] )
> : str("-",_f(n)[4])
> );
>
> // Handle fractional part
> // Returns vector
> // [numerator, denominator, fraction as float, string fraction, x=depth]
> function _f(n,num=1,den=2,tolerance=1/128/2, maxDen=128, x=1) = (
> let (f=num/den, an=abs(n), r=an-f )
> //echo(n=n,str(num,"/",den,"=", f, ",r=",r), x )
> ( an == f )
> ? //echo("match")
> [ num, den, f, r, str(num,"/",den), x ]
> : ( x>32 || abs(r) <= tolerance )
> ? //echo("x or tol",x=x,r=r)
> [ num, den, f, r, str("~",num,"/",den, (r>0)?"+":"",r ), x ]
> : ( an < f )
> ? ( den >= maxDen )
> ? //echo("MaxDen")
> [ num, den, f, r, str("~",num,"/",den, (r>0)?"+":"",r ), x ]
> : _f(n,num*2-1,den*2,x=x+1)
> : _f(n,num+1,den,x=x+1)
> );
>
>
>
> echo(fractional(0.125));
> echo(fractional(0.128));
> echo(fractional(0.124));
> echo("1/3:",fractional(1/3));
> echo("pi:",fractional(3.1415926));
>
> echo("-2:",fractional(-2));
> echo("-1.25:",fractional(-1.25));
> echo("-1/8:",fractional(-1/8));
> echo("1/128:",fractional(1/128));
> echo("1/127:",fractional(1/127));
> echo("9/20:",fractional(9/20));
> echo("-11/8-1/256",fractional(-11/8-1/256));
> echo("-1-3/8-1/256:",fractional(-1-3/8-1/256));
> echo(49/128,-49/128); // +-0.382812
> echo( -(1+49/128)); // -1.38281
> echo( -(1+49/128-0.00390625) ); // -1.37891
>
> testN=[1:2:16];
> testD=[4:4:32];
> for (n=testN,d=testD) {
> echo(str(n,"/",d),fractional(n/d));
> }
>
> echo("99:",fractional(99));
> echo("0:",fractional(0));
> echo("-0:",fractional(-0));
> echo("1:",fractional(1));
-----
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
--
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19238.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
R
Ronaldo
Sat, Nov 19, 2016 6:36 PM
Sorry, Michael, but the rest of division isn't correct for negative numbers
yet.
echo("-1-5/8+1/256",-1-5/8+1/256,fractional(-1-5/8+1/256));
echo(-1-5/8-0.00390625); // fractional output
//ECHO: "-1-5/8+1/256", -1.62109, "-1 & ~5/8-0.00390625"
//ECHO: -1.62891
I had not unveiled your code. Too much cases for my taste :)
I have written a code based on the method I generally use with a calculator.
It just does the calculations. The fancy string outputs may be drawn from
its output. It seems to work with negative numbers too.
function frac(x, tolerance=0.0005, MaxDen=7) =
let( n = min(MaxDen, ceil(log(1/tolerance)/log(2))), // I would not
use tolerances but...
y = ([for(i=[n:-1:1]) if(ceil(xpow(2,i))%2!=0) [xpow(2,i),
i]]),
y0 = len(y)==0 ? x: y[0][0], // best odd representation
y1 = len(y)==0 ? 0: y[0][1], // power of best odd representation
div = pow(2, y1), // divisor
int = sign(y0)*floor( abs(y0) / div ), // integer part
num = ceil(y0) % div, // numerator
rest = -(ceil(y0)-y0)/div) // rest of division
[ int, num, div, rest ];
Sorry, Michael, but the rest of division isn't correct for negative numbers
yet.
echo("-1-5/8+1/256",-1-5/8+1/256,fractional(-1-5/8+1/256));
echo(-1-5/8-0.00390625); // fractional output
//ECHO: "-1-5/8+1/256", -1.62109, "-1 & ~5/8-0.00390625"
//ECHO: -1.62891
I had not unveiled your code. Too much cases for my taste :)
I have written a code based on the method I generally use with a calculator.
It just does the calculations. The fancy string outputs may be drawn from
its output. It seems to work with negative numbers too.
> function frac(x, tolerance=0.0005, MaxDen=7) =
> let( n = min(MaxDen, ceil(log(1/tolerance)/log(2))), // I would not
> use tolerances but...
> y = ([for(i=[n:-1:1]) if(ceil(x*pow(2,i))%2!=0) [x*pow(2,i),
> i]]),
> y0 = len(y)==0 ? x: y[0][0], // best odd representation
> y1 = len(y)==0 ? 0: y[0][1], // power of best odd representation
> div = pow(2, y1), // divisor
> int = sign(y0)*floor( abs(y0) / div ), // integer part
> num = ceil(y0) % div, // numerator
> rest = -(ceil(y0)-y0)/div) // rest of division
> [ int, num, div, rest ];
--
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19250.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Mon, Nov 21, 2016 5:28 AM
Well I'll be embarrassed of this one is wrong... has happened before :|
This tends to have reduced fractions (at least much of the time)
I would not have been able to debug this without functional echo().
// Display numbers as fraction of inch
// as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128
// released to the public domain
// there may be a better way.
//
// Handle negative & whole numbers
function fractional(n) = (
let( fn=floor(n), cn=ceil(n), an=abs(n) )
//echo(str("f(",n,")") )
( an == floor(an) )
? str(n) // whole number (+ or -)
: ( n > 1 ) // has +integer & proper fraction
? str(fn," & ",_f(n-fn)[4] ) // [4] is the textual part
: ( n > 0 ) // just proper fraction
? _f(n-fn)[4]
: ( n < -1 ) // has -integer & proper fraction
? str(cn," & ",_f((n-cn))[4] )
: str("-",_f(n)[4] ) // negative proper fraction
);
// Handle fractional part
// Returns vector
// [numerator, denominator, remainder, float fraction+remain., string
fraction + remain.]
function _f(n,num=1,den=2,tolerance=1/128/2, maxDen=128, x=1) = (
let (sn=(n==0)?+1:sign(n), f=snnum/den, r=n-f )
//echo(n=n, str(num,"/",den,"=f=", f, ",r=",r, ", f+r=",f+r), sn=sn, x )
( n == f ) // exact fraction
? [ num, den, r, f+r, str(num,"/",den)]
: ( x>32 || abs(r) <= tolerance )
? [ num, den, r, f+r, str(num,"/",den, (r>0)?"+":"", r)]
: ( abs(n) < abs(f) )
// n smaller than fraction so increase denominator, unless max
reached
? ( den >= maxDen )
? [ num, den, r, f+r, str(num,"/",den, (r>0)?"+":"", r) ]
: _f(n,num2-1,den*2,x=x+1)
// n larger so increase numerator
: _f(n,num+1,den,x=x+1)
);
echo("-1-5/8+1/256",-1-5/8+1/256, fractional(-1-5/8+1/256));
echo("-1-5/8-1/256",-1-5/8-1/256, fractional(-1-5/8-1/256));
echo("126/127:",126/127,127/128-6.15157e-005, fractional(126/127));
echo("-126/127:",-126/127,-127/128+6.15157e-005, fractional(-126/127));
echo("9/20:",9/20,29/64-0.003125, fractional(9/20));
echo("-11/8-1/256",-11/8-1/256,-1-3/8-0.00390625,
fractional(-11/8-1/256));
echo("-1.25:", fractional(-1.25));
echo("0.125:", fractional(0.125));
echo("0.128:", fractional(0.128));
echo("0.124:", fractional(0.124));
echo("1/3:", fractional(1/3));
echo("pi:",3 + 9/64+0.0009676, fractional(3.1415926));
echo("-2:", fractional(-2));
echo("-1/8:", fractional(-1/8));
echo("1/128:",fractional(1/128));
echo("-1-3/8-1/256:",-1-3/8-1/256,-1 - 3/8-0.00390625,
fractional(-1-3/8-1/256));
/*
echo("----------------");
testN=[1:2:16];
testD=[4:4:32];
for (n=testN,d=testD) {
echo(str(n,"/",d, "=",n/d),fractional(n/d));
}
echo("99:",fractional(99));
echo("0:",fractional(0));
echo("-0:",fractional(-0));
echo("1:",fractional(1));
*/
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19280.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Well I'll be embarrassed of this one is wrong... has happened before :|
This tends to have reduced fractions (at least much of the time)
I would not have been able to debug this without functional echo().
> // Display numbers as fraction of inch
> // as common hardware measures x/2,x/4,x/8,x/16,x/32,x/64,x/128
> // released to the public domain
> // there may be a better way.
> //
>
> // Handle negative & whole numbers
> function fractional(n) = (
> let( fn=floor(n), cn=ceil(n), an=abs(n) )
> //echo(str("f(",n,")") )
> ( an == floor(an) )
> ? str(n) // whole number (+ or -)
> : ( n > 1 ) // has +integer & proper fraction
> ? str(fn," & ",_f(n-fn)[4] ) // [4] is the textual part
> : ( n > 0 ) // just proper fraction
> ? _f(n-fn)[4]
> : ( n < -1 ) // has -integer & proper fraction
> ? str(cn," & ",_f((n-cn))[4] )
> : str("-",_f(n)[4] ) // negative proper fraction
> );
>
> // Handle fractional part
> // Returns vector
> // [numerator, denominator, remainder, float fraction+remain., string
> fraction + remain.]
>
> function _f(n,num=1,den=2,tolerance=1/128/2, maxDen=128, x=1) = (
> let (sn=(n==0)?+1:sign(n), f=sn*num/den, r=n-f )
> //echo(n=n, str(num,"/",den,"=f=", f, ",r=",r, ", f+r=",f+r), sn=sn, x )
> ( n == f ) // exact fraction
> ? [ num, den, r, f+r, str(num,"/",den)]
> : ( x>32 || abs(r) <= tolerance )
> ? [ num, den, r, f+r, str(num,"/",den, (r>0)?"+":"", r)]
> : ( abs(n) < abs(f) )
> // n smaller than fraction so increase denominator, unless max
> reached
> ? ( den >= maxDen )
> ? [ num, den, r, f+r, str(num,"/",den, (r>0)?"+":"", r) ]
> : _f(n,num*2-1,den*2,x=x+1)
> // n larger so increase numerator
> : _f(n,num+1,den,x=x+1)
> );
>
> echo("-1-5/8+1/256",-1-5/8+1/256, fractional(-1-5/8+1/256));
> echo("-1-5/8-1/256",-1-5/8-1/256, fractional(-1-5/8-1/256));
> echo("126/127:",126/127,127/128-6.15157e-005, fractional(126/127));
> echo("-126/127:",-126/127,-127/128+6.15157e-005, fractional(-126/127));
> echo("9/20:",9/20,29/64-0.003125, fractional(9/20));
> echo("-11/8-1/256",-11/8-1/256,-1-3/8-0.00390625,
> fractional(-11/8-1/256));
>
> echo("-1.25:", fractional(-1.25));
> echo("0.125:", fractional(0.125));
> echo("0.128:", fractional(0.128));
> echo("0.124:", fractional(0.124));
> echo("1/3:", fractional(1/3));
> echo("pi:",3 + 9/64+0.0009676, fractional(3.1415926));
>
> echo("-2:", fractional(-2));
> echo("-1/8:", fractional(-1/8));
> echo("1/128:",fractional(1/128));
>
> echo("-1-3/8-1/256:",-1-3/8-1/256,-1 - 3/8-0.00390625,
> fractional(-1-3/8-1/256));
> /*
> echo("----------------");
> testN=[1:2:16];
> testD=[4:4:32];
> for (n=testN,d=testD) {
> echo(str(n,"/",d, "=",n/d),fractional(n/d));
> }
>
> echo("99:",fractional(99));
> echo("0:",fractional(0));
> echo("-0:",fractional(-0));
> echo("1:",fractional(1));
> */
-----
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
--
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19280.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Mon, Nov 21, 2016 6:47 AM
And if you were wondering why...
ECHO: cI = 103.83, cO = 119.538, ID = 33.05, "1 & 39/128-0.0035064", OD =
38.05, "1 & 1/2-0.0019685"
Seems o-rings and seals are in more varied imperial sizes and they often are
shown in fractional, not decimal inches, 38.05mm ~= 1 & 1/2 inch
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19281.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
And if you were wondering why...
ECHO: cI = 103.83, cO = 119.538, ID = 33.05, "1 & 39/128-0.0035064", OD =
38.05, "1 & 1/2-0.0019685"
Seems o-rings and seals are in more varied imperial sizes and they often are
shown in fractional, not decimal inches, 38.05mm ~= 1 & 1/2 inch
-----
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
--
View this message in context: http://forum.openscad.org/Output-inch-hardware-sizes-tp19204p19281.html
Sent from the OpenSCAD mailing list archive at Nabble.com.