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

Allow multiplying by Double, not just Int #53

Open
sargunv opened this issue Jan 8, 2025 · 3 comments · May be fixed by #60
Open

Allow multiplying by Double, not just Int #53

sargunv opened this issue Jan 8, 2025 · 3 comments · May be fixed by #60
Labels
enhancement New feature or request
Milestone

Comments

@sargunv
Copy link

sargunv commented Jan 8, 2025

I'm doing this, to generate a list of stops for a scale bar on a map:

private fun buildStops(mantissas: List<Length>, exponents: IntRange) = buildList {
  for (e in exponents) for (m in mantissas) add(m * 10.0.pow(e))
}

val metricStops = buildStops(mantissas = listOf(1, 2, 5).map { it.meters }, exponents = -1..7)

It generates a list like

0.1 m
0.2 m
0.5 m
1 m
2 m
5 m
10 m
...
10000 km
20000 km
50000 km

but 10.0.pow(e) is a Double, and this library only has an operator overload for Length x Int. So I needed to add this helper:

private operator fun Length.times(other: Double) =
  (this.toLong(LengthUnit.International.Nanometer) * other).roundToLong().nanometers

Such a helper should probably be provided by the library. Looking at kotlin.time.Duration, they already have this:

public operator fun times(scale: Double): Duration {
    val intScale = scale.roundToInt()
    if (intScale.toDouble() == scale) {
        return times(intScale)
    }

    val unit = storageUnit
    val result = toDouble(unit) * scale
    return result.toDuration(unit)
}

If this is acceptable, I'd be happy to PR it.

@kevincianfarini
Copy link
Owner

I think we'd actually want this implemented on SaturatingLong so that it can be used anywhere. Want to take a stab at that?

@kevincianfarini
Copy link
Owner

Actually, I take that back. The sample from Duration looks sufficient for implementing on quantities. Mind PRing for all the types?

@kevincianfarini kevincianfarini added this to the 0.2.0 milestone Jan 8, 2025
@sargunv
Copy link
Author

sargunv commented Jan 8, 2025

Will do

@sargunv sargunv changed the title Allow multiplying by Number, not just Int Allow multiplying by Number or Double, not just Int Jan 8, 2025
@sargunv sargunv changed the title Allow multiplying by Number or Double, not just Int Allow multiplying by Double, not just Int Jan 8, 2025
@kevincianfarini kevincianfarini added the enhancement New feature or request label Jan 15, 2025
@sargunv sargunv linked a pull request Jan 27, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants