Solve Mathematica Truncation Problem with Vectors of Numbers

  • Mathematica
  • Thread starter docfan123
  • Start date
  • Tags
    Mathematica
In summary, the conversation discusses a problem where Mathematica truncates some numbers when creating a table for further calculation. The solution is to use the NumberForm function to specify the desired number of digits for each value in the table. This ensures that all values are displayed correctly.
  • #1
docfan123
2
0
I've this problem i don't know how to solve ... I declare this two vectors of numbers(a shortened list) and then when I make a table with them for further calculation, Mathematica truncates some of them, not showing the decimal part of the numbers with more than 6 digits ... The code is:

VX = {-12, -11, -10, -9, -8, -7, -6};
VY = {891628, 528522.1, 297984, 158106.1, 77812, 34804.5, 13729.6};

Points = {VX, VY};
Mat := Transpose[Points];

Print["Table of values: "];
Print[TableForm[Mat, TableHeadings -> {None, {"X", "Y"}},
TableSpacing -> {1, 4}, TableAlignments -> Right]];

And the results :

X Y
-12 891628
-11 528522.
-10 297984
-9 158106.
-8 77812
-7 34804.5
-6 13729.6

see the Y's corresponding to X=-11 and -9, doesn't have the decimal part as declared :(
How i do make it right ?
Thanks, very much
 
Physics news on Phys.org
  • #2
Use NumberForm[].

In[18]:= NewMat=Map[NumberForm[#,{8,2}]&,Mat,{-1}];

In[19]:= FullForm[NewMat]

Out[19]//FullForm= List[
List[NumberForm[-12,List[8,2]],NumberForm[891628,List[8,2]]],
List[NumberForm[-11,List[8,2]],NumberForm[528522.1`,List[8,2]]],
List[NumberForm[-10,List[8,2]],NumberForm[297984,List[8,2]]],
List[NumberForm[-9,List[8,2]],NumberForm[158106.1`,List[8,2]]],
List[NumberForm[-8,List[8,2]],NumberForm[77812,List[8,2]]],
List[NumberForm[-7,List[8,2]],NumberForm[34804.5`,List[8,2]]],
List[NumberForm[-6,List[8,2]],NumberForm[13729.6`,List[8,2]]]]

In[20]:= Print["Table of values: "];
Print[TableForm[NewMat,TableHeadings->{None,{"X","Y"}},
TableSpacing->{1,4},TableAlignments->Right]];

From In[20]:=Table of values:

From In[20]:=
X Y
-12 891628.
-11, 528522.1
etc.
 
  • #3
Thanks a lot! It worked fine...
 

Related to Solve Mathematica Truncation Problem with Vectors of Numbers

1. How do I solve a Mathematica truncation problem with vectors of numbers?

To solve a Mathematica truncation problem with vectors of numbers, you can use the built-in function "Truncate" which truncates all numbers in a vector to a specified number of digits. The syntax for this function is Truncate[vector, n], where "vector" is the vector of numbers and "n" is the number of digits to truncate to.

2. What is truncation in Mathematica?

In Mathematica, truncation refers to the process of removing or cutting off a specified number of digits from a number. This can be useful when working with large numbers or when you only need a certain level of precision in your calculations.

3. Can I truncate numbers to a specific decimal place in Mathematica?

Yes, you can use the built-in function "Round" to round numbers to a specific decimal place. The syntax for this function is Round[number, n], where "number" is the number to be rounded and "n" is the decimal place to round to.

4. How do I truncate a vector of numbers to different decimal places?

You can use the "Map" function to apply the "Truncate" function to each element in the vector. The syntax for this is Map[Truncate[#, n]&, vector], where "n" is the desired number of digits to truncate to and "vector" is the vector of numbers.

5. What happens if I truncate a number to more digits than it has?

If you truncate a number to more digits than it has, Mathematica will return the original number without any changes. This is because there are no additional digits to truncate.

Back
Top