Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618286
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:14:43+00:00 2026-05-22T02:14:43+00:00

Is it possible to somehow create a pow function for measure types? The pow

  • 0

Is it possible to somehow create a pow function for measure types?
The pow function in f# only takes int as parameter, and then pow function in the Math class takes a float – but dosent allow float<cm>.

I first thought that:

let rec myPow(x:float<cm>,y:int) =
    if y = 0 then x
    else myPow(x*x, y - 1)

might work out, but its obvious that each time it come across the else line it will change the return type.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:14:44+00:00Added an answer on May 22, 2026 at 2:14 am

    Ankur is correct – you cannot do this (without resorting to hacks that would break units).

    Maybe a clearer description of the problem is that the type of pow function would depend on the value of the argument and F# doesn’t allow you to do this. You could imagine this would work if were using just literals as the second argument, but it would become tricky if you used expressions:

    pow a 3 // Assuming a = 1.0<cm>, the return type is float<cm ^ 3>
    pow a n // Assuming a = 1.0<cm>, the return type is float<cm ^ n>
    

    In the second case the value n would have to appear in the type!

    You can use some nasty tricks (inspired by this Haskell article), but it becomes a bit crazy. Instead of using numeric literals, you’d use something like S(S(S(N))) to represent the number 3. This way, you can bring the number into the type. You probably don’t want to do this, but here is an example:

    [<Measure>] type cm
    
    // Represents a number with units of measure powered to the
    // number's value (e.g "(S (S O))" has type Num<cm, cm^3>)
    type Num<[<Measure>] 'M, [<Measure>] 'N> = 
      | O_ of int * float<'N>
      | S_ of int * Num<'M, 'N / 'M>
    
    // Constructors that hide that simplify the creation  
    let O : Num<'M, 'M> = O_ (1, 0.0<_>)
    let S n = match n with O_(i, _) | S_(i, _) -> S_(i + 1, n)
    
    // Type-safe power function with units of measure
    let pow (x:float<'M>) ((O_(i, _) | S_(i, _)):Num<'M, 'M 'N>) : float<'M 'N> =
      // Unsafe hacky implementation, which is hidden
      // from the user (for simplicity)
      unbox ((float x) ** float i)
    
    let res = pow 2.0<cm> (S (S O))
    

    EDIT: I posted the source code to F# snippets, so that you can see the inferred types: http://fssnip.net/4H

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an optimized function to count Math.pow(a,b) % c; in Javascript;
Is it possible somehow to create in-memory database in SQLite and then destroy it
i am asking myself if it is possible to somehow create a dynamic gui
is it somehow possible to call a rails function or to access a rails
Is it somehow possible to create a stored procedure when using SQLite?
Is is possible in C to create structs inline? typedef struct { int x;
I know it's possible to separate to create a pointer to member function like
Is it somehow possible to create a tab menu in the style of the
Is it possible to somehow create a custom @synthesize to generate custome getter, setters
Does somebody know if is it possible somehow to create a custom NSAnimationCurve so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.