Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fraction sometime get converted, sometime not. Don't want them converted #2576

Open
nodiaque opened this issue Nov 14, 2024 · 7 comments
Open
Labels
enhancement New feature or request stalebot-enabled The PR/issue is marked to go stale over time

Comments

@nodiaque
Copy link

Description
When adding ingredient, sometime fraction are required like 2/3 cup of water or 1/4 tablespoon of salt. The fraction 1/4 stay as 1/4 once saved but the 2/3 get transformed to 0.67. I want the fraction to stay and not have the decimal number (in fact, I want what I enter to stay). It seems to work for 1/2, 1/4, 3/4 but not 1/3, 2/3.

Also, when adding quantity, don't show 0/1

Reproduction
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Actual behavior
Image
Should be 2/3 not 0.67

After changing quantity, now displaying a 0/1:
Image

Screenshots
Recipe edit:
Image

Browser
Edge

Versions
Nextcloud server version: 30.0.2
Cookbook version: 0.11.2
Database system: PostgreSQL

@nodiaque nodiaque added the bug Something isn't working label Nov 14, 2024
@christianlupus
Copy link
Collaborator

This comes from the recalculation algorithm.

What would be valid fractional values? How about 2/3? 7/8? 22/15? 2135/52351? At some point we need to make a cut and either round or go with decimals. If you have a good suggestion on how to handle this, then feel free to state that. We can then see if this is implementable.

@christianlupus christianlupus added enhancement New feature or request stalebot-enabled The PR/issue is marked to go stale over time and removed bug Something isn't working labels Dec 2, 2024
@nodiaque
Copy link
Author

nodiaque commented Dec 2, 2024

There's a way to save fraction reel value in database. There's surely already library available to do that. I used to do it in native PHP back in 2001 so surely there's something today that's able to do that, specially with all the other website that are able to.

Maybe already save more then 2 decimal. Save like 6, since nobody will ever require 6 decimal, you check 5 decimal and if it match, it's a fraction. That would be a way of doing it, not the prettiest.

@christianlupus
Copy link
Collaborator

I think we misunderstand. Apart from the fact that the values are not stored ATM in the database, we are not storing the numeric value at all. We are just storing a list of strings like 100g of flour, 1/2 cup of milk, or a pinch of salt. From there, we need to extract the numeric value and the unit. (This comes from the schema.org standard that we use internally.)

The problem comes into play, once we have the recalculation algorithm in place. The user in the frontend simply can configure a scaling factor for all ingredients.

  • If the original string contains a decimal number, we can use that and scale it according to the factor. No big problem.
  • If the original string does not contain any numeric information (a pinch of salt), it is not scaled but currently a warning is printed. Ok so far.
  • If the original string has a fraction (either a unicode fraction ½ or something of the form 1/2), for simple factors (e.g. 1.5) we can keep the fractions ¾. If the factor is more complex, what should we do? We have to obey the user's request (to some extend).

The last can cause trouble: Wht if the user requests a factor of 12.042? Do you really want to print 1503/250 cups of milk? Don't you think that 6.02 would be more clear?

@nodiaque
Copy link
Author

nodiaque commented Dec 3, 2024

I would already say don't convert any fraction. It's very easy to scale fraction, there's a lot of math function ready to do that.

And if it's not a fraction, let it not a fraction.

What you could do is put the quantity in a separate box. If none is present, don't scale cause its a string like pinch. This way you don't have to search for the number. That's usually how I see it in other recipe program.

@christianlupus
Copy link
Collaborator

OK, let's separate implementation from expected behavior. First, we need to define the desired behavior. How to implement this can be addressed later. I just wanted to give you insight why some claims of yours are plainly useless as these were based on wrong assumptions.

Nevertheless, what do you mean by "not convert any fractions"?

  • Disable the auto converter once a fraction is detected?
  • Just do not convert a line that contains a fraction (so that the ingredients might get out of sync)?
  • Multiply the fractions mathematically risking high order fractions?
  • Try to round the fractions to more "handlabel" ones (501/1000 is roughly 1/2)? How much error can we allow?
  • Convert to digital representaton at some point? What is the cutoff?

If you have more ideas, feel free to add them.

@christianlupus
Copy link
Collaborator

Self-note: One can use chain-fractions eventually for approximations.

@nodiaque
Copy link
Author

nodiaque commented Dec 3, 2024

Fraction convert isn't hard to do. You can do it with 3 simple mathematical expression. Let's say you have 3/4 and you want 3 times that? You want 3 * 3/4 which give 9/4 (you can extract the numerator and denominator quite easily). Then, you do 9 / 4 and keep only integer part of 2,25 which is 2 (function already exist for that) and then you use mod function with 9 mod 4 = 1. Result? 2 1/4. That's how you normally convert fraction without needing to caculate.

After, if you want to reduce the fraction to the smallest possible like 2/4 = 1/2, there's multiple mathematic way to do so but I'm pretty sure there's already math library available to do all of that. If someone enter 501/1000, it's not 1/2, it's 1/1000 more then 1/2. Don't forget you are dealing with recipe, you don't see stuff like that in recipe. If someone try to go this way, just don't support it, it's useless to try to do everything for a recipe. Already, there's nothing in real life that will give you the ability to measure 501/1000. Recipe come in /2, /3, /4 and /8. Sometime you see the other one, but there's not even tools to measure these (at least for home user). You could go with the GCF method using only 2,3,4 and 8, which imply a maximum of 8 calculation per fraction (but using some mathematic logic, you can have it do most of the time only 1 step).

I would never convert a fraction to a digital representation. At worst, don't touch it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stalebot-enabled The PR/issue is marked to go stale over time
Projects
None yet
Development

No branches or pull requests

2 participants