Skip to content

Commit

Permalink
spreadFloat defaults to 0 - 1 range
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Mar 19, 2021
1 parent 8919a45 commit dd38ec3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions src/gen-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ const Util = require('./utility.js');
// Generate a list of n-length starting at one value
// up until (but excluding) the 3th argument.
// Evenly spaced values in between in floating-point
// Defaults to range of 0 - 1 for Float
//
// @params {array-length, low-output, high-output}
// @return {Array}
// @param {Int+} -> array-length
// @param {Int} -> low output
// @param {Int} -> high output
// @return -> {Array}
//
function spreadFloat(len=1, lo=len, hi){
function spreadFloat(len=1, lo=1, hi){
// if hi undefined set lo to 0 and hi=lo
if (hi === undefined){ var t=lo, lo=0, hi=t; }
// calculate the range
Expand All @@ -29,6 +32,7 @@ function spreadFloat(len=1, lo=len, hi){
lo = Math.min(lo, hi);
// len is minimum of 1
len = Math.max(1, len);
if (len === 1){ return [0]; }
// stepsize
let s = Math.abs(r) / len;
// generate array
Expand All @@ -44,11 +48,12 @@ exports.spreadF = spreadFloat;
// Generate a list of n-length starting at one value
// up until (but excluding) the 3th argument.
// Set an exponential curve in the spacing of the values.
// Defaults to range of 0 - 1 for Float
//
// @params {length, low-output, high-output, exponent}
// @return {Array}
//
function spreadFloatExp(len=1, lo=len, hi, exp=1){
function spreadFloatExp(len=1, lo=1, hi, exp=1){
// if hi undefined set lo to 0 and hi=lo
if (hi === undefined){ var t=lo, lo=0, hi=t; }
// calculate the range
Expand All @@ -57,6 +62,7 @@ function spreadFloatExp(len=1, lo=len, hi, exp=1){
lo = Math.min(lo, hi);
// len is minimum of 1
len = Math.max(1, len);
if (len === 1){ return [0]; }
// generate array
let arr = [];
for (let i=0; i<len; i++){
Expand All @@ -71,7 +77,7 @@ exports.spreadFloatExp = spreadFloatExp;
// @params {length, low-output, high-output}
// @return {Array}
//
function spread(len, lo, hi){
function spread(len, lo=len, hi){
let arr = spreadFloat(len, lo, hi);
return arr.map(v => Math.floor(Number(v.toPrecision(15))));
}
Expand All @@ -82,7 +88,7 @@ exports.spread = spread;
// @params {length, low-output, high-output, exponent}
// @return {Array}
//
function spreadExp(len, lo, hi, exp){
function spreadExp(len, lo=len, hi, exp){
let arr = spreadFloatExp(len, lo, hi, exp);
return arr.map(v => Math.floor(Number(v.toPrecision(15))));
}
Expand All @@ -91,11 +97,12 @@ exports.spreadExp = spreadExp;
// Generate a list of n-length starting at one value
// ending at the 3th argument.
// Evenly spaced values in between in floating-point
// Defaults to range of 0 - 1 for Float
//
// @params {length, low-output, high-output}
// @return {Array}
//
function spreadInclusiveFloat(len=1, lo=len, hi){
function spreadInclusiveFloat(len=1, lo=1, hi){
// if hi undefined set lo to 0 and hi=lo
if (hi === undefined){ var t=lo, lo=0, hi=t; }
// calculate the range
Expand All @@ -104,6 +111,7 @@ function spreadInclusiveFloat(len=1, lo=len, hi){
lo = Math.min(lo, hi);
// len is minimum of 1
len = Math.max(1, len);
if (len === 1){ return [0]; }
// stepsize
let s = Math.abs(r) / (len - 1);
// generate array
Expand All @@ -119,11 +127,12 @@ exports.spreadIncF = spreadInclusiveFloat;
// Generate a list of n-length starting at one value
// ending at the 3th argument.
// Set an exponential curve in the spacing of the values.
// Defaults to range of 0 - 1 for Float
//
// @params {length, low-output, high-output, exponent}
// @return {Array}
//
function spreadInclusiveFloatExp(len=1, lo=len, hi, exp=1){
function spreadInclusiveFloatExp(len=1, lo=1, hi, exp=1){
// if hi undefined set lo to 0 and hi=lo
if (hi === undefined){ var t=lo, lo=0, hi=t; }
// calculate the range
Expand All @@ -146,7 +155,7 @@ exports.spreadInclusiveFloatExp = spreadInclusiveFloatExp;
// @params {length, low-output, high-output}
// @return {Array}
//
function spreadInclusive(len, lo, hi){
function spreadInclusive(len, lo=len, hi){
var arr = spreadInclusiveFloat(len, lo, hi);
return arr.map(v => Math.floor(Number(v.toPrecision(15))));
}
Expand All @@ -158,7 +167,7 @@ exports.spreadInc = spreadInclusive;
// @params {length, low-output, high-output, exponent}
// @return {Array}
//
function spreadInclusiveExp(len, lo, hi, exp){
function spreadInclusiveExp(len, lo=len, hi, exp){
var arr = spreadInclusiveFloatExp(len, lo, hi, exp);
return arr.map(v => Math.floor(Number(v.toPrecision(15))));
}
Expand Down
62 changes: 33 additions & 29 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs');
let entryPoint = "../index";
// entryPoint = "../build/ts.bundle.js";
// entryPoint = "../build/ts.es5.js";
entryPoint = "../build/ts.es5.min.js";
// entryPoint = "../build/ts.es5.min.js";

const Srl = require(entryPoint);
const Gen = Srl.Generative;
Expand Down Expand Up @@ -60,14 +60,14 @@ fullTest();
function fullTest(){
console.time('Total Time');

testSerial();
// testSerial();
testGen();
testAlgo();
testRand();
testMod();
testStat();
testTranslate();
testUtil();
// testAlgo();
// testRand();
// testMod();
// testStat();
// testTranslate();
// testUtil();

pagebreak("All Tests Passed");
console.timeEnd('Total Time');
Expand Down Expand Up @@ -95,7 +95,9 @@ function testGen(){
pagebreak("Generative");

test("Gen.spread()");
test("Gen.spreadFloat(7)");
test("Gen.spreadFloat()");
test("Gen.spreadInclusive()");
test("Gen.spreadInclusiveFloat()");

test("Gen.spread(5)");
test("Gen.spread(5, 12)");
Expand All @@ -106,32 +108,34 @@ function testGen(){
test("Gen.spreadInclusive(5, 12)");
test("Gen.spreadInclusive(5, 3, 12)");
test("Gen.spreadInclusive(5, 12, 3)");

// test("Gen.spreadInclusive(5, 7, 19)");
// test("Gen.spreadInc(4, 10, 2)");

test("Gen.spreadFloat(5, 2, 11)");
test("Gen.spreadFloat(4)");
test("Gen.spreadFloat(4, 2)");
test("Gen.spreadFloat(4, -1, 1)");
test("Gen.spreadF(4, -2, 2)");

test("Gen.spreadInclusiveFloat(9, -1, 1)");
test("Gen.spreadIncF(9, -1, 1)");
test("Gen.spreadInclusiveFloatExp(5, 0, 1, 2)");

test("Gen.fill()");
test("Gen.fill(10, 2, 15, 3, 20, 4)");
test("Gen.spreadInclusiveFloat(5)");
test("Gen.spreadInclusiveFloat(5, 2)");
test("Gen.spreadInclusiveFloat(5, -1, 1)");

test("Gen.spreadExp(10, 0, 10, 2)");
test("Gen.spreadInclusiveExp(10, 0, 10, 2)");
test("Gen.spreadFloatExp(12, 0, 10, 0.5)");
test("Gen.spreadInclusiveFloatExp(12, 0, 10, 0.5)");

// test("Gen.fill()");
// test("Gen.fill(10, 2, 15, 3, 20, 4)");

test("Util.plot(Gen.sineFloat(16), {log: false, height: 5, data: true})");
test("Gen.sineFloat(10, 1, -1, 1, 0.5)");
// test("Gen.sin(8)");
test("Util.plot(Gen.cosineFloat(16), {log: false, height: 5, data: true})");
// test("Gen.cos(8)");
// test("Util.plot(Gen.sineFloat(16), {log: false, height: 5, data: true})");
// test("Gen.sineFloat(10, 1, -1, 1, 0.5)");
// // test("Gen.sin(8)");
// test("Util.plot(Gen.cosineFloat(16), {log: false, height: 5, data: true})");
// // test("Gen.cos(8)");

test("Util.plot(Gen.sine(10), {log: false})");
test("Gen.sine(12, 1, -1, 1)");
test("Util.plot(Gen.sine(11, 4, 0, 7), {log: false})");
// test("Util.plot(Gen.sine(10), {log: false})");
// test("Gen.sine(12, 1, -1, 1)");
// test("Util.plot(Gen.sine(11, 4, 0, 7), {log: false})");

test("Util.plot(Gen.cosine(7, 1.5), {log: false})");
// test("Util.plot(Gen.cosine(7, 1.5), {log: false})");
}

function testAlgo(){
Expand Down

0 comments on commit dd38ec3

Please # to comment.