GithubHelp home page GithubHelp logo

go-calculator's Introduction

Calculator Package

Version License

The go-calculator package provides a powerful Calculate function for performing basic math calculations with a string input.

Table of Contents



Installation

To install the latest version of the go-calculator package, use the following command:

go get -u github.com/brianlewyn/go-calculator

How does it work?

The calculator function assigns a unique identity to each character in the input string. Numbers are maintained in their original form, while other characters are treated as tokens to minimize memory usage. These identities are then stored in a linked list, allowing for left-to-right reading control.

The function validates the grammar of the expression and solves the calculations based on the hierarchy of operations. Numbers are converted when necessary, while retaining their original form as strings when not involved in operations.

Usage

The Calculate function takes a string expression as input and returns the calculated result and any potential errors.

Example

package main

import (
   "fmt"
   "log"

   "github.com/brianlewyn/go-calculator"
)

func main() {
    expression := "(0.5 + 4.5 - 1) * 10 * √(6-2) / 4^2"
    res64, err := basic.Calculate(expression)
    if err != nil {
        log.Fatal(err)
    }

	fmt.Printf("[ %s ] = %.2f", expression, res64)
}

Output

[ (0.5 + 4.5 - 1) * 10 * √(6-2) / 4^2 ] = 5.00

Examples

Basic Calculations

Calculations Results
2 2
5+5 10
5%2 1
5^3 125
√√625 5

Complex Calculations

Calculations Results
(1+2+(3+4+(5+6+(7+8)+9+10)+11+12)+13+14) 105
(-1-2-(-3-4-(-5-6-(-7-8)-9-10)-11-12)-13-14) -15
√(2^2 + 3^2 + 4^2 + 5^4) + √(2π) + 156 -1 117.08005197971984

License

The Calculator Package is open-source and released under the MIT License. Feel free to use, modify, and distribute this package according to the terms of the license.

Thank you for choosing the Calculator Package. I hope it proves to be a valuable tool in your projects!

go-calculator's People

Contributors

brianlewyn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.