Group Abstract Group Abstract

Message Boards Message Boards

0
|
12.2K Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Changing return type of BaseForm into something computable?

Posted 10 years ago
POSTED BY: Bob Stephens
5 Replies
Posted 10 years ago

Ok, I was referring to programmatically detecting type. Agree that it is nice that the notebook shows the subscript as the result of the calculation and the documentation saying "print" infers that the return type however that stuff is only useful if you are treating the Mathematica notebook as a line by line calculator.

I'm looking for the notebook to simply have a little more intelligence built-in for detecting and providing guidance with types. Note the following:

  1. If the Mathematica notebook can "automagically" lookup the number of bananas consumed in the Olympics for an arbitrary year and can convert Boltzmann's Constant into a computable quantity as shown below (although here again with nested fuctions), the notebook and kernel/object-framework should be a little more intelligent in terms of managing variable types,

    K = QuantityMagnitude[UnitConvert[Quantity["BoltzmannConstant"]]];
    
  2. I simply want the notebook to elegantly help me discover types and options for operating on types without the need to look for (missing) type information in the documentation while coding in-line. So it would just be nice if the notebook saw that I was trying to add a BaseForm (which obviously deals with numerical values by definition) and an integer and displayed the type mismatch with a conversion hint for BaseForm like "did you mean use "First[BaseForm]?" which Mathematical already does well in other instances.

  3. Thanks for your example overloading the plus operator but that example (agree) seems unsafe as it could globally impact that operator. A similar example is Apple's Swift programming language where it is relatively simple to do the following in a (much more) type safe (and non global) manner:

    A similar example to yours in Swift would be to do the following
    "abc" * 5 = "abcabcabcabcabc"
    
    Using a function prototype like this (but this solution is more type safe as it is not unlocking the * operator globally)
    
    let aString = "abc"
    let largerString = aString * 5
    
    func *(lhs: String, rhs: Int) -> String {
    
    
    var result = lhs
    
        for _ in 2...rhs {
    
        result += lhs
    
        }
    
    return result
    }
    

Thanks

POSTED BY: Bob Stephens

Mathematica already notifies you that the output is special:

enter image description here

Also:

??Base

gives:

enter image description here

notice that it says print...

I'm not sure what else you want them do? Your expression Baseform[...] - 400 is equivalent to "Hello" - 400, it doesn't make much sense...

Of course you could add a rule:

Unprotect[Plus]
Plus[BaseForm[a_, b_], c_] := a + c

BaseForm[100,16]+12 
112

But be VERY careful changing the definition of Plus! It is protected for a reason!

POSTED BY: Sander Huisman
Posted 10 years ago
POSTED BY: Bob Stephens

As has been said in the other post: basically you should enter your numbers like one of the three methods:

a=16^^1123FA0
a=FromDigits["1123FA0", 16]
a=ToExpression["16^^1123FA0"]

Then do all your computations with it, and finally show your results using BaseForm. Note that BaseForm should be considered as a function only to show your result in different bases. You should not use it to 'store' different numbers; bad idea! It will make life very confusing!

POSTED BY: Sander Huisman
Posted 10 years ago

Just to clarify, my old post on this subject Base Form Computations used a similar solution I am just trying to find out if Mathematica 11 has any new features to make computations less tedious when moving between number bases. If I can ask WolframAlpha how many bananas were consumed during the Olympics using natural language and get visual summaries, I would expect Mathematica to be able to automatically detect computations being done between values in different number bases and at least provide some suggested options in the notebook for functions.

Here's a suggestion, perhaps adding an attribute to BaseForm (and other functions) that would permit changing the default return type to something computable such as......BaseForm[ <input value and number base>, <output number base>, <computable boolean flag>] where the return value from BaseForm would be the actual value instead of the BaseForm structure/list. Just a thought.

Thanks!

POSTED BY: Bob Stephens
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard