Message Boards Message Boards

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

Changing return type of BaseForm into something computable?

Posted 8 years ago

I'm trying to do a large number of computations where we want to

  1. take the output of BaseForm converting Hexadecimal values to Decimal
  2. show the intermediate result of a computation in Decimal
  3. convert the intermediate result from [2] back to Hexadecimal using BaseForm

I am able to generate a prototype of the desired result Manually Using The First[] Function but this is not the desired method as this requires the annoying step of having to know that the first item in the list which makes up the BaseForm[] return type is the value I want to convert.

Print["Here's the decimal value ", dec = BaseForm[16^^FFFFD188,10]]
Print["What type is the return from BaseForm? It is this -->   ", Head[dec]]
Print["This manual step Head[First[dec]]] gives me an integer type  ", Head[First[dec]]]
Print["And now we can show the result of the desired calculation in decimal  ", desiredCalculation = First[dec]-3072000]
Print["Now show the result back in Hex  ",BaseForm[desiredCalculation,16]]
Print["It would be nice if Mathematica could detect what do do in the first place..... dec2 = BaseForm[16^^FFFFD188,10]-400....but it can't -->", dec2 = BaseForm[16^^FFFFD188,10]-400]

Here's the decimal value 4294955400

What type is the return from BaseForm? It is this --> BaseForm

This manual step Head[First[dec]]] gives me an integer type Integer

And now we can show the result of the desired calculation in decimal 4291883400

Now show the result back in Hex Subscript[ffd0f188, 16]

It would be nice if Mathematica could detect what do do in the first place..... dec2 = BaseForm[16^^FFFFD188,10]-400....but it can't -->-400+4294955400

POSTED BY: Bob Stephens
5 Replies
Posted 8 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

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 8 years ago

Hi Sander,

Sure, I somewhat expected that nothing had changed for this in Mathematica 11. I guess my expectation is at a minimum the help documentation should document the return type in the help info - since Mathematica is essentially functional programming, the entire documentation set can be viewed as function API's that should include well documented return types.

More optimistically as a mentioned above, since Mathematica now has the help tips while typing in a notebook and since the kernel/framework in this example has enough type information available, the notebook input cell should be able to at least provide some guidance or provide the user with type info when trying to do dec2 = BaseForm[16^^FFFFD188,10]-400.... instead of just returning this result -400+4294955400.

For now agree with treating BaseForm[] as a "display only" function with BaseForm return type.

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 8 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract