Group Abstract Group Abstract

Message Boards Message Boards

1
|
31.9K Views
|
18 Replies
|
17 Total Likes
View groups...
Share
Share this post:

How to define constants?

Posted 9 years ago
POSTED BY: Werner Geiger
18 Replies
POSTED BY: Carl Woll
Posted 9 years ago

Just an example how this could be done:

declareConstant[name_Symbol, value_] := (name = value; Protect@name;
  Print[ToString@Unevaluated@name <> " = " <> ToString@name])

Then:

enter image description here

To organize the constants they could all be declared in a separate context, say Constants`. But at the moment I don't get the code for this to work.

POSTED BY: Hans Milton

seems to me it's a lot easier to define your constant as a variable and then write your code so it doesn't get changed.

POSTED BY: Frank Kampas
Posted 9 years ago

Sorry Frank, but this is not the point.

If someone ever wrote a large program with some 100.000 lines of code and some 10 developers and had a lot of global variables therein, he knows what a nightmare that becomes. Global variables are a nightmare in any case because of their far reaching unknown influence. And to expect some global variable to stay unchanged, i.e. a constant, is practically nonsense. But even if you assure by naming conventions or the like, that everybody knows, what is global and what is even constant, you will never succeed. Since people do not respect rules which they can break.

That is why many tens of years ago somebody invented objected-oriented programming. Just to shield data from being changed unexpectedly.

POSTED BY: Werner Geiger
POSTED BY: Daniel Lichtblau
Posted 9 years ago

I had to read your comment several times to understand it. Now I think your comment ist helpful, and we agree. Apart from "encapsulating globals into lists or associations or anything else". For me it doesn't change anything in which data structure you store global data. As long as those globals are freely, i. e. directly available as pure data to everybody they are dangerous. They have to be encapsulated into objects/functions. Probably you ment to say the same.

Hence: Defining constants as ordinary, but protected variables would be the best way in WL? I feel so as well.

Those Macro-mechanisms known from standard language preprocessors, witch are nothing but text-processing are obviously not available in WL.

POSTED BY: Werner Geiger
Posted 9 years ago
POSTED BY: Werner Geiger
Anonymous User
Anonymous User
Posted 9 years ago
POSTED BY: Anonymous User
POSTED BY: Sander Huisman
Posted 9 years ago

Whow! Your coding looks horrible and unreadable without careful reengineering. What an effort just for a simple constant.

Why not something simple like the following? Should be within WL of course, but if that or something similar does not exist there, then self written as lower case "constant"-function with help of your SetAttributes... from above.

cIdxStart=Constant[1];
cStrStart=Constant[Row[{"Start, i=",cIdxStart}]]; 
POSTED BY: Werner Geiger
POSTED BY: Sander Huisman
Posted 9 years ago

Hmm, @Sander, this seems to be more difficult than I thought at first.

I want to have some named object, e.g. myConst, after being defined to stay constant throughout some scope, say a notebook or a notebook-section or the like, and being replaced in my code by its value - whatever that is - whenever this name appears. Similar to With[{myConst=...},...]. But without bracketing all that scope.

Defined by something like myConst=Constant[...].

At first I thought that your Protect-mechanism for a normal variable myConst would do all I want. But now after reading your comment and thinking about what all the other programming languages do, I am no more sure. Maybe I just want to have this kind of pure text-replacement which classical languages like C, C++ do during their pre-compile processing.

POSTED BY: Werner Geiger

Hello Sander,

why do you write

N[MyConstant[],p_:{MachinePrecision, MachinePrecision}] := N[Log[2],p]

the MachinePrecison twice?

POSTED BY: Hans Dolhaine

The default value is exactly that:

N~Default~2
{MachinePrecision, MachinePrecision}

So I just copied that...

POSTED BY: Sander Huisman

You should take a look at Contexts and generally some quite educational set of tutorials Modularity and the Naming of Things.

POSTED BY: Sam Carrettie
Posted 9 years ago

I use two different methods. If it is a few constants that really will be constant, and I am wiling to re-evaluate the entire notebook to change them, then I do as Frank suggests and just set their values at the beginning of the notebook in a well defined place. On the other hand, if I want different values at different places, or if I want to preserve the ability to work with them symbolically, I define my set of constants with a list of rules, and use replace to access them when numerical values are wanted.

In[5]:= m = 2; b = -1;

In[6]:= constants = {mm -> 2, bb -> -1};

In[7]:= m 3 + b

Out[7]= 5

In[8]:= mm 3 + bb /. constants

Out[8]= 5
POSTED BY: David Keith

n = 2;

POSTED BY: Frank Kampas

And in addition, one could set the appropriate Attributes like e.g. Pi:

SetAttributes[n,{Constant,Protected}]
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard