Feature request: fract()
Nature of issue?
- [ ] Found a bug
- [ ] Existing feature enhancement
- [x] New feature request
Most appropriate sub-area of p5.js?
- [ ] Color
- [ ] Core/Environment/Rendering
- [ ] Data
- [ ] Events
- [ ] Image
- [ ] IO
- [x] Math
- [ ] Typography
- [ ] Utilities
- [ ] WebGL
- [ ] Other (specify if possible)
New feature details:
I think that having a fract() function to return the fractional part of a number might be a nice addition to the math functions. I was reading through the book of shaders and it was showing how fract() is used in generating noise and I thought it would be a cool function to have as a part p5js.
I get that if someone wanted this function they could easily write it themselves as its just returning what is passed as a paramater minus the floor of that value, but I feel like it fits in with the rest of the math functions.
Anyway, let me know what you think about adding fract().
Thanks!
It's not really needed as (n%1).toFixed(4) works well.
https://stackoverflow.com/a/4512317/45966
It's not really needed as (n%1).toFixed(4) works well.
https://stackoverflow.com/a/4512317/45966
I recognize that there are easy ways for people to implement fract() on their own, but there other functions in p5js that are just as easy to implement. You could argue that a function like sq() isn't really needed because it just returns the value passed through multiplied by itself.
If this feature is to be added, I would like to do it.
@ScottGrogin Is this issue still open? I would like to work on this asap if possible. Please let me know which file to change. Thank you
What is the proposed implementation? Because with something like (n%1).toFixed(4) although practically it probably would not cause much problem, you are limited in terms of the number of precision after the decimal point, not to mention that toFixed returns a string and not a number (so does it belongs with String Functions such as nf or Math Functions such as floor?) Simply doing n%1 would cause floating point precision issue which is why the stackoverflow solution have the toFixed portion.
@vedhant @ankur54 If any of you are interested in looking into implementing this, I would suggest fleshing out what your proposed implementation would be in this issue first before heading straight into the source code. Otherwise the likelyhood of said PR is rejected is higher when you did not have a clear idea about the implementation that the community has looked at, because there could very likely be edge cases that you didn't account for that we would have otherwise spotted and fixed while in the discussion phases here on the issue.
toFixed belongs to Number native, and does return a string.
you are limited in terms of the number of precision after the decimal point
Totally agree
Ok, so my initial idea was to use (n%1).toFixed(4) and then convert it back to number use parseInt(x, 10). So, the overall function would look something like this:
parseInt((n%1).toFixed(4).slice(2), 10)
Although, I could not find any corner cases, if the above function does not satisfy, we can write a function that does something similar to long division method, i.e., takes the remainder of the number, multiplies it by 10 divides it, take the quotient again takes the remainder and repeat all over again until required precision.
How does both of the idea look like?
@ankur54 parseInt((n%1).toFixed(4).slice(2), 10) would not work because if you make n = 1.0004 for example, it returns 4. Something like parseFloat((n % 1).toFixed(4), 10) would have worked better (why slice the number?) However, it still doesn't solve the problem of lost precision (parseFloat((1.00004 % 1).toFixed(4).slice(2), 10) will simply return 0 instead of 0.00004)
takes the remainder of the number, multiplies it by 10 divides it, take the quotient again takes the remainder and repeat all over again until required precision.
I don't quite understand this can you maybe explain with a simple example?
n = 1.0004
Oh, shoot!!! I forgot that case. Maybe you can give me some examples, to better understand what kind of output you would expect. Also, in case of your example, what do you expect output to be? Is it 0004 or .0004? If its the former one, can you show pending zeros, before a number in case of an int data type?
I don't quite understand this can you maybe explain with a simple example?
Maybe the code would be a clearer way to explain. But before that can you please clear my doubts?
Can we also consider the results for the input values 1e20 and 1e-20?
I'm not sure what to expect which is kinda the point to the discussion.
Basically: Should the function return a string or a number? If it is to return a number, it has to return 0.0004 in the case of n = 1.0004 because there is only a single number type in Javascript and that is a 64-bit floating point number. If it is to return a string then we can ask if we return the decimal point or not.
I can't really give an exhaustive list of example or rather I can't be sure that I have given such a list of examples that is complete, at least not without solving the above problem first. However, you can consider below some numbers that may be passed to the function:
0.1
1.5
1.00004
0.00005
1e20 // as suggested by Spongman
1e-20 // as suggested by Spongman
5
0.3-0.2 // in js it evaluates to 0.09999999999999998
// there probably are many more
function scientificToDecimal(num) {
//if the number is in scientific notation remove it
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
var zero = '0';
parts = String(num).toLowerCase().split('e'), //split into coeff and exponent
e = parts.pop(),//store the exponential part
l = Math.abs(e), //get the number of zeros
sign = e/l,
sgn = parts[0][0],
coeff_array = parts[0].split('.');
if(sign === -1) {
coeff_array[0] = Math.abs(coeff_array[0]);
num = sgn + zero + '.' + new Array(l).join(zero) + coeff_array.join('');
}
else {
var dec = coeff_array[1];
if(dec) l = l - dec.length;
num = sgn + coeff_array.join('') + new Array(l+1).join(zero);
}
}
return num;
};
num = parseInt(n, 10)
n = scientificToDecimal(n);
if(n > num)
console.log(n - num)
else
console.log(num - n)
How about this?
@limzykenneth Please comment on the code.
@ankur54 Have you tried some examples with the code above? What do they return? Honestly, most of this is not a matter of how to implement the feature in code but rather to determine what the code should be returning when provided with numbers that can be interpreted in different ways (mainly because of the floating point nature of Javascript Number).
PS. There's no need to ping me on a separate comment, I (and others as well) may not always be available to comment immediately but we will get notifications whenever you post a new comment on an issue we are already participating in.
I'm going to have a think about this, if anyone else have an idea do let us know.
I have checked it with a lot of examples, for which, it returns the correct answer. Although the only issue here is, it returns a String instead of a floating point, which I prefer, because, in cases of large floating point numbers, parseFloat() changes it to scientific representation. Also, sorry for explicitly pinging you.
@ankur54 Running the code you provided and setting n = 1e-20 returns 9.
@ScottGrogin In your case, are you thinking of getting the fractional digits as a string or as a number?
I have tweaked the code a little bit.
function scientificToDecimal(num) {
//if the number is in scientific notation remove it
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
var zero = '0';
parts = String(num).toLowerCase().split('e'), //split into coeff and exponent
e = parts.pop(),//store the exponential part
l = Math.abs(e), //get the number of zeros
sign = e/l,
sgn = parts[0][0],
coeff_array = parts[0].split('.');
if(sign === -1) {
coeff_array[0] = Math.abs(coeff_array[0]);
num = sgn + zero + '.' + new Array(l).join(zero) + coeff_array.join('');
}
else {
var dec = coeff_array[1];
if(dec) l = l - dec.length;
num = sgn + coeff_array.join('') + new Array(l+1).join(zero);
}
}
return num;
};
n = 126.8232325346e40;
n = scientificToDecimal(n);
num = (typeof n == "string") ? n.split('.')[1] : n.toString().split('.')[1];
if(num == undefined) num = "0000";
console.log('.' + num)
I am sorry if my solution is not up to the mark, but it seems you are looking for something different as a solution. It would be helpful if you could tell me.
@ankur54 No don't worry, actually this is much better. Its behaviour seems to be pretty consistent.
I'm not looking for anything in particular other than an idea about how to deal with unexpected numbers, not really in terms of code (although it can be), but rather given any particular input what the output should be. Which means answering questions like "should the function return a string or a number", "how should the function handle scientific notated numbers", "how should the function handle cases where there are lost of precision due to floating point arithmetic", etc.
There isn't a right or wrong way of answering those questions as they can be dealt with one way or another, for example, the function can return a string or a number and either will be fine but it is a matter of which is the preferred solution. I haven't find myself looking to use this functionality so I can't say one way or the other is better so that's why I need input from other people including you to give an opinion on what is preferred.
@ankur54 Running the code you provided and setting
n = 1e-20returns9.@ScottGrogin In your case, are you thinking of getting the fractional digits as a string or as a number?
Definitely thinking that it should be number and not a string.
Hello ! If I may add one suggestion , I think for negative numbers , we must return 1 minus the fractional part of the number, For eg : fract(1.3) = 0.3 fract(-1.3) = 0.7 The current suggested code returns 0.3 for both cases. Hopefully , it's of help !