{"id":25212459,"url":"https://github.com/baitcode/cairo-fixed-point-arithmetic","last_synced_at":"2026-01-23T22:22:48.209Z","repository":{"id":271868545,"uuid":"914819148","full_name":"baitcode/cairo-fixed-point-arithmetic","owner":"baitcode","description":"Fixed Point Arithmetic type for Cairo smart contracts","archived":false,"fork":false,"pushed_at":"2025-02-11T23:31:03.000Z","size":225,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T00:26:39.226Z","etag":null,"topics":["cairo","fixed-point","fixed-point-arithmetic","smart-contracts","starknet"],"latest_commit_sha":null,"homepage":"https://scarbs.xyz/packages/fp","language":"Cairo","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baitcode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-10T11:20:58.000Z","updated_at":"2025-02-04T11:50:30.000Z","dependencies_parsed_at":"2025-02-12T00:23:48.814Z","dependency_job_id":"f6d3c397-ac2c-4b38-b653-26f3f905c495","html_url":"https://github.com/baitcode/cairo-fixed-point-arithmetic","commit_stats":null,"previous_names":["baitcode/cairo-fixed-point-arithmetic"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/baitcode/cairo-fixed-point-arithmetic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baitcode%2Fcairo-fixed-point-arithmetic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baitcode%2Fcairo-fixed-point-arithmetic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baitcode%2Fcairo-fixed-point-arithmetic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baitcode%2Fcairo-fixed-point-arithmetic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baitcode","download_url":"https://codeload.github.com/baitcode/cairo-fixed-point-arithmetic/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baitcode%2Fcairo-fixed-point-arithmetic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28701640,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T17:25:48.045Z","status":"ssl_error","status_checked_at":"2026-01-23T17:25:47.153Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cairo","fixed-point","fixed-point-arithmetic","smart-contracts","starknet"],"created_at":"2025-02-10T15:01:55.015Z","updated_at":"2026-01-23T22:22:48.193Z","avatar_url":"https://github.com/baitcode.png","language":"Cairo","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1 align=\"center\"\u003eCairo Fixed Point Arithmetic\u003c/h1\u003e\n  \u003ch3 align=\"center\"\u003eUses 123 bit for integer part and 128 for fractional and packs everything in one felt252 \u003c/h3\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/baitcode/cairo-fixed-point-arithmetic/refs/heads/main/assets/fixed_point_arithmetics.webp\" height=\"200\"\u003e\n  \u003cbr /\u003e\n\n  \u003cdiv style=\"text-align: center;\"\u003e  \n  \u003ca href=\"https://github.com/baitcode/cairo-fixed-point-arithmetic/issues/new?assignees=\u0026labels=bug\u0026template=bug_report.md\u0026title=bug%3A+\"\u003eReport a Bug\u003c/a\u003e\n  -\n  \u003ca href=\"https://github.com/baitcode/cairo-fixed-point-arithmetic/issues/new?assignees=\u0026labels=enhancement\u0026template=new_feature.md\u0026title=feat%3A+\"\u003eRequest a Feature\u003c/a\u003e\n  -\n  \u003ca href=\"https://github.com/baitcode/cairo-fixed-point-arithmetic/discussions\"\u003eAsk a Question\u003c/a\u003e\n  \u003c/div\u003e\n  \u003cbr /\u003e\n\n\u003c/div\u003e\n\n## About\n\nThis crate was born in an effort to create fixed point datatype for [Ekubo Protocol](https://ekubo.org/) governance. It's specifically taiored to be used in smart-contracts. This library features:\n\n- Fixed Point datatype that supports operations: multiplication, division, addition and substraction.\n- Data packing and unpacking for smart contract storage.\n- Overflow and underflow checking for all operations.\n- Additional convenince methods for better performance without conversions such as `u64 by u128 division` method.\n- Conversion from `u64`, `u128` and `u256` types.\n- Rounding implementation.\n- Exponent function calculation added: f(x) = e^x\n- High precision PI and E constants\n\nMain type this crate export is:\n\n```cairo\n#[derive(Debug, Drop, Copy, Serde)]\npub struct UFixedPoint123x128 { \n    value: u256\n}\n```\n\nDespite the fact this library don't derive `starknet::Store` it provides `fp::UFixedPoint123x128StorePacking` implementation for use in contract storage.\n\n## Usage\n\nAll use-cases are perfectly described by this snippet.\n\n```cairo\n// Basic type import\nuse fp::{ UFixedPoint123x128 };\n\n// Store packing implementation import\nuse fp::{ UFixedPoint123x128StorePacking };\n\n// Convenience functions to avoid type conversions\nuse fp::{\n    div_u64_by_u128, \n    div_u64_by_fixed_point, \n    mul_fixed_point_by_u128\n};\n\n// Exponent import\nuse fp::exp::{ exp_power_static, exp_power_dyn };\n\nfn main() {\n    // Create a fixed point value 1.0\n    let one: UFixedPoint123x128 = 1_u64.into();\n    // Create a fixed point value 100.0\n    let hundred: UFixedPoint123x128 = 100_u64.into();\n    // Calculate a fixed point value 0.01\n    let one_over_hundred = one / hundred;\n    // OR\n    let other_example = div_u64_by_u128(1, 100);\n\n    assert_eq!(one_over_hundred, other_example );\n    \n    let multiplication_is_supported = one_over_hundred * hundred;\n    assert_eq!(multiplication_is_supported, 1_u64.into());\n    \n    let two: UFixedPoint123x128 = 2_u64.into();\n    let three: UFixedPoint123x128 = 3_u64.into();\n    let six: UFixedPoint123x128 = 6_u64.into();\n    let one_over_three = one / three;\n    let one_over_six = one / six;\n\n    // PartialEq is implemented, values are equal if they are close enough.\n    // Difference is less than 1 / 2^124\n    assert_eq!(one_over_three, one_over_six * two);\n\n    // Calculate square of exponent \n    let e_pow_2 = exp_power_static(two); // calculate e^2\n}\n```\n\nThis crate also provides additional method for `UFixedPoint123x128` type through public \n`UFixedPoint123x128Impl` which implements `UFixedPointTrait`. \n\n```cairo\n// Additional methods implementation\nuse fp::{ UFixedPoint123x128Impl };\n\nUFixedPoint123x128Impl implements UFixedPointTrait \n\ntrait UFixedPointTrait {\n    // Returns integer part of the fixed point value\n    fn get_integer(self: UFixedPoint123x128) -\u003e u128;\n\n    // Returns fractional part of the fixed point value\n    fn get_fractional(self: UFixedPoint123x128) -\u003e u128;\n    \n    // Rounds fixed point and returns integer part. 0.5 is rounded up.\n    fn round(self: UFixedPoint123x128) -\u003e u128;\n}\n```\n\n## Contributing\n\n[WIP] but feel free to open issue, discuss it with me and submit PR upon assignment.\n\n## Special Thanks\n\n\u003cmarkdown-accessiblity-table data-catalyst=\"\"\u003e\u003ctable\u003e\n\u003ctbody\u003e\n\u003ctr\u003e\n\u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\n\u003ca href=\"https://github.com/shramee\"\u003e\n\u003cimg src=\"https://avatars.githubusercontent.com/u/11048263?v=4?v=4?s=100\" width=\"100px;\" alt=\"Shramee Srivastav\" style=\"max-width: 100%;\"\u003e\n\u003cbr\u003e\n\u003csub\u003e\u003cb\u003eShramee Srivastav\u003c/b\u003e\u003c/sub\u003e\n\u003c/a\u003e\n\u003cbr\u003e\n\u003c/td\u003e      \n\u003ctd align=\"center\" valign=\"top\"\u003e\nFor submitting bug report on unchecked error during packing due to incorrect MAX_INT value.\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/markdown-accessiblity-table\u003e\n\n## PS\n\nLove and peace, dude. Hope it was useful.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaitcode%2Fcairo-fixed-point-arithmetic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaitcode%2Fcairo-fixed-point-arithmetic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaitcode%2Fcairo-fixed-point-arithmetic/lists"}