Tcl 8.5: custom math functions & arbitrary precision integers

(Printer friendly version)

In Tcl 8.5 you can now define, at the script level (instead of needing to write in C) custom math functions. For example here is a factorial function:

proc tcl::mathfunc::fact {n} {
    if {![string is integer -strict $n] || $n < 0} then {
        return -code error "fact is only defined for non-negative integers"
    }
    set result 1
    # remember, n! (n factorial) is the product of all integers between 1 and n
    # except 0! which is defined to be 1
    for {set i 2} {$i <= $n} {incr i} {
        incr result [expr {$result * $i}]
    }
    return $result
}

Then we can use fact($n) in expr, if, while, for, etc. just like any of the "built-in" math functions.

Since we now have arbitrary precision integers (thanks Kevin!) we can compute factorials that exceed the native size of a long integer, i.e., 42!

% expr fact(42)
1405006117752879898543142606244511569936384000000000

Defining your own math functions makes solutions to Project Euler problems easier to write. YMMV. The other features of Tcl 8.5 will probably be exciting to an even wider audience.

As of January 2008 the Google calculator can only calculate up to 170!. We can do twice as better quite quickly (timings from my 2.4 GHz MacBook Pro laptop):

% time {expr {fact(171)}} 1000
295.348728 microseconds per iteration
% time {expr {fact(340)}} 1000
791.435306 microseconds per iteration

—Michael A. Cleverly

Comments

Happy Easter: [ mail | www | link ]
Easter Day Images Happy easter Images Happy easter Quotes Easter Day Images 2017 Easter Day Messages Easter Day Quotes Easter Day Pictures Easter Day Wallpapers Happy Easter Sunday Images Happy Easter Pictures Happy Easter Wallpapers Happy Easter Messages Happy Easter Pics Happy Easter Eggs Happy Easter Shayari Easter day 2017 Images Easter Day Images Quotes Happy Easter Sunday Happy easter Sunday Images Happy Easter Sunday 2017 Happy Easter Day Images

Sun, 26 Feb 2017, 01:47

Re: [ mail | www | link ]
cctv vendors | hikvision dvr price in mumbai | panasonic cctv india | sony cctv camera dealers in mumbai | cctv camera price in mumbai india | honeywell cctv dealers in mumbai |

Sun, 23 Dec 2018, 12:18

fejki: [ mail | www | link ]
housekeeping contract in mumbai | housekeeping services in andheri | housekeeping vendors | housekeeping services in india | housekeeping companies in india | hospital housekeeping services |

Fri, 05 Apr 2019, 05:31

Diago Devilla: [ mail | www | link ]
https://diagodevilla.contently.com/ http://www.webestools.com/profile-124313.html https://www.zumvu.com/diagodevilla/about/ https://support.dream-theme.com/users/diago-devilla/ http://innerdesign.com/user/Diago-Devilla

Sun, 08 Sep 2019, 05:58

Call Girls in Gurgaon: [ mail | www | link ]
Call Girls in Gurgaon
Call Girl Gurgaon
Call Girls Gurgaon
Call Girl in Gurgaon
Gurgaon Call Girls
Gurgaon Call Girl
Gurgaon Escort
Gurgaon Escorts
escort in Gurgaon
escorts in Gurgaon
escorts in Gurgaon
Call Girls in Dwarka
Call Girl Dwarka
Call Girls Dwarka
Call Girl in Dwarka
Dwarka Call Girls
Dwarka Call Girl
Dwarka Escort
Dwarka Escorts
escort in dwarka
escorts in dwarka
escorts in dwarka
Call Girls in Gurgaon
Call Girl Gurgaon
Call Girls Gurgaon
Call Girl in Gurgaon
Gurgaon Call Girls
Gurgaon Call Girl
Gurgaon Escort
Gurgaon Escorts
escort in Gurgaon
escorts in Gurgaon
escorts in Gurgaon
Call Girls in sikanderpur gurgaon
Call Girls in sohna road gurgaon
Call Girls in mg road gurgaon
Call Girls in iffco chowk gurgaon
Call Girls in Haridwar
Call Girls in sushant lok
Call Girls in Gurgaon
Call Girls in Dehradun
Call Girls in electronic city
Call Girls in Noida
call girls in mumbai
plumbing company Toronto
plumbing company Mississauga
quickbooks support phone number
plumbing company Toronto

Tue, 24 Dec 2019, 01:36

james thomas: [ mail | www | link ]
While i just had become within your online site while sporting specified recognition comfortably a lot of touch submits. Amiable technique for outlook, I will be book-marking at present need devices gain spgs best suited all the way up. Rolls Royce rental

Wed, 22 Jan 2020, 10:30

james thomas: [ mail | www | link ]
The design surely marvelous. Every one of those modest material are actually fashioned utilising wide range of the past past experiences. I'd like the lot tremendously. popular fish in florida

Tue, 28 Jan 2020, 15:14

daska: [ mail | www | link ]
There's lots of dissertation internet websites via the internet at the time you get unsurprisingly discussed as part of your page. Auto verkaufen

Sat, 08 Feb 2020, 03:46

swisswatch: [ mail | www | link ]
Nineteen sixty-nine was an important touchstone for the watch industry,as it was the year of what%92s been dubbed%93the great automatic chronograph race.%94A handful of Swiss brands%97and one notable Japanese one%97vied to become the first manufacturer to produce and market a wristwatch chronograph watch with automatic winding.The results of this competition produced a number of Replica Watches that are today regarded as icons,such as the replica breitling watches Chrono-Matic,Zenith El Primero,and Heuer Monaco.

Fri, 05 Jun 2020, 01:00

tur: [ mail | www | link ]
compare spread betting companies spread betting how it works spread betting website spreadbetting demo spread bet companies points spread

Sun, 07 Jun 2020, 16:17

alex: [ mail | www | link ]
It's a very useful blog. Thank you for getting a lot of information. 카지노사이트 파워볼사이트 토토사이트 안전놀이터 안전한놀이터 메이저사이트 비아그라구매

Thu, 17 Sep 2020, 02:13

Julieth Felizola: [ mail | www | link ]
SEMrush Review Users Advice Users Advice work from home adsense approval which is the best web hosting adsense approval bluehost review users Advice education blogs abcya users advice education blogs product product users advice see sent facebook requests users advice education write an answer users advice users advice users advice users advice users advice users advice users advice mentor education users advice users advice photos users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice educational blogs educational blogs users advice educational tutorials science and technology trends users advice modern classroom for study users advice users advice abcya education topic users advice users advice users advice users advice prodigy math game users advice kahoot education education users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice users advice read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer read my answer

Thu, 08 Oct 2020, 09:27

custom closets sarasota: [ mail | www | link ]
This page certainly has all of the info I wanted about this subject and didn't know who to ask. custom closets sarasota

Wed, 28 Oct 2020, 00:45

pool repair bradenton: [ mail | www | link ]
There's certainly a lot to find out about this issue. I really like all the points you made. pool repair bradenton

Wed, 28 Oct 2020, 00:50

sarasota photographer: [ mail | www | link ]
You've made some decent points there. I looked on the web for additional information about the issue and found most people will go along with your views on this website. sarasota photographer

Wed, 28 Oct 2020, 00:54

outdoor kitchens & bbq el dorado hills: [ mail | www | link ]
Good post. I learn something new and challenging on blogs I stumbleupon every day. It will always be exciting to read through articles from other writers and use something from their web sites. outdoor kitchens & bbq el dorado hills

Wed, 28 Oct 2020, 00:57

gainesville kitchen remodeling: [ mail | www | link ]
I blog quite often and I genuinely appreciate your information. The article has really peaked my interest. gainesville kitchen remodeling

Wed, 28 Oct 2020, 01:00

soft washing lakewood ranch: [ mail | www | link ]
Pretty! This was a really wonderful post. Thanks for providing these details. soft washing lakewood ranch

Wed, 28 Oct 2020, 01:04

amazon.com/mytv: [ mail | www | link ]
www.amazon.com/mytv enter code www.amazon.com/mytv enter code amazon.com/mytv amazon.com/mytv amazon.com/mytv www.amazon.com/mytv amazon.com/mytv primevideo.com/mytv xfinity.com/authorize pbs.org/activate amazon.com/mytv enter code www.amazon.com/mytv www.amazon.com/mytv login amazon.com/mytv amazon.com/mytv amazon.com/mytv amazon.com/mytv amazon.com/mytv primevideo.com/mytv amazon.com/redeem amazon.com/redeem amazon.com/mytv primevideo.com/mytv amazon.com/mytv amazon.com/mytvv amazon.com/mytv amazon.com/mytv amazon.com/redeem amazon.com/mytv amazon.com/mytv amazon.com/mytv amazon.com/mytv

Wed, 16 Dec 2020, 09:24

&#47085;&#53412;&#44152; Lucky girl: [ mail | www | link ]

https://www.ekffo150.com/theking https://www.ekffo150.com/merit https://www.ekffo150.com/yes https://www.ekffo150.com/first https://www.ekffo150.com/sands https://www.ekffo150.com/joy https://www.ekffo150.com/royal https://www.ekffo150.com/starclub https://www.ekffo150.com/asian https://www.ekffo150.com/superman https://www.ekffo150.com/gatsby https://www.ekffo150.com/33 https://www.ekffo150.com/oncasino https://www.ekffo150.com

https://www.ajp4949.com/theking https://www.ajp4949.com/merit https://www.ajp4949.com/sands https://www.ajp4949.com/first https://www.ajp4949.com/33casino https://www.ajp4949.com/worldcasino https://www.ajp4949.com/korea https://www.ajp4949.com/

https://www.bgj4949.com/ https://www.bgj4949.com/sands https://www.bgj4949.com/theking https://www.bgj4949.com/first https://www.bgj4949.com/yes https://www.bgj4949.com/33 https://www.bgj4949.com/world https://www.bgj4949.com/super https://www.bgj4949.com/korea https://www.bgj4949.com/gatsby

https://www.dnfl4949.com/ https://www.dnfl4949.com/first https://www.dnfl4949.com/merit https://www.dnfl4949.com/theking https://www.dnfl4949.com/sands https://www.dnfl4949.com/yescasino https://www.dnfl4949.com/gatsby https://www.dnfl4949.com/corea https://www.dnfl4949.com/33casino https://www.dnfl4949.com/f1casino

Tue, 16 Feb 2021, 18:31

&#47085;&#53412;&#44152; Lucky girl: [ mail | www | link ]
우리카지노사이트 예스카지노 퍼스트카지노 더킹카지노 메리트카지노 샌즈카지노 조이카지노 로얄카지노 스타클럽카지노 아시안카지노 슈퍼맨카지노 개츠비카지노 33카지노 온라인카지노 우리카지노사이트 퍼스트카지노 더킹카지노 메리트카지노 샌즈카지노 33카지노 월드카지노 코리아카지노 우리카지노사이트 샌즈카지노 예스카지노 퍼스트카지노 더킹카지노 33카지노 월드카지노 슈퍼카지노 코리아카지노 개츠비카지노 우리카지노사이트 퍼스트카지노 메리트카지노 더킹카지노 샌즈카지노 예스카지노 개츠비카지노 코리아카지노 33카지노 f1카지노

Tue, 16 Feb 2021, 18:33

Caregiver Alzheimers: [ mail | www | link ]
After going over a few of the blog posts on your web page, I really appreciate your technique of blogging. Caregiver Alzheimers

Wed, 10 Mar 2021, 23:11

pool resurfacing el dorado hills: [ mail | www | link ]
And he in fact bought me breakfast simply because I found it for him... lol. So allow me to reword this.... pool resurfacing el dorado hills

Tue, 16 Mar 2021, 20:01

Screen Repair Sarasota: [ mail | www | link ]
After going over a few of the blog posts on your web page, I really appreciate your technique of blogging. Screen Repair Sarasota

Tue, 16 Mar 2021, 20:13

well water testing Boone, NC: [ mail | www | link ]
Everything is very open with a clear clarification of the issues. It was definitely informative. Your website is very helpful. Thanks for sharing! well water testing Boone, NC

Thu, 18 Mar 2021, 23:58

Leave a comment

Name:  
Email: (optional)
URL: (optional)

Your comment: