We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Evaluating e^x
Evaluating e^x
Sort by
recency
|
211 Discussions
|
Please Login in order to post a comment
ex :: Double -> Double
ex x = sum [x^n / fromIntegral (product [1..n]) | n <- [0..] :: [Int]]
Here's a quick way from fapello to implement the solution in Python:
python Copy Edit import math
Function to calculate e^x using a series expansion
def calculate_exp(x, n_terms=100): result = 0 for i in range(n_terms): result += (x ** i) / math.factorial(i) return result
Input reading
t = int(input()) for _ in range(t): x = float(input()) print(f"{calculate_exp(x):.4f}") This computes 𝑒 𝑥 e x using the series expansion up to 100 terms, ensuring high accuracy within the tolerance limits provided.
Be sure to handle empty lines on the input!
Haskel code