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

feat: implement Exp trait for generic exponentiation #1203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion math/exp.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub fn exp(input : Double) -> Double {
trait Exp {
exp(Self) -> Double
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the expected return type of exp function?
should it be Self, Double or Float?

Copy link
Contributor

@hackwaly hackwaly Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Self is a proper choose.

If (1 : Int).exp(x) need a Double result, users always can turn it to Double first.

1.to_double().exp(x)


pub impl Exp for Double with exp(inp) { inp.exp() }

pub impl Exp for Int with exp(inp) { inp.to_double().exp() }

pub fn exp[T : Exp](input : T) -> Double {
input.exp()
}
7 changes: 5 additions & 2 deletions math/math.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn ceil(Double) -> Double

fn cos(Double) -> Double

fn exp(Double) -> Double
fn exp[T : Exp](T) -> Double

fn floor(Double) -> Double

Expand Down Expand Up @@ -44,6 +44,9 @@ fn trunc(Double) -> Double
// Type aliases

// Traits

trait Exp
// Extension Methods
impl Exp for Int

impl Exp for Double