Message Boards Message Boards

0
|
4599 Views
|
8 Replies
|
3 Total Likes
View groups...
Share
Share this post:

'N' is not accurate if I write to a file?

Posted 7 years ago

because when I write an array to a file it is not equal to the result that I see on the screen?

$MaxPrecision = 32;
$fil = OpenWrite["E:\\number.txt"];
a = {0.0011111111111111111111111111111111`32., 2.6};
SumLev[k_] := N[IntegerPart[Max[k]] + Total[FractionalPart[k]], 2];
arcCos[{i_Real, k_?Positive}] := {ArcCos[i], SumLev[{k, 4.2}]}; 
arcCos[_] = {0, 0};
$a = arcCos[a]
Write[$fil, $a]
Close[$fil];

Out: {1.5696852154551616956931498094999, 4.8}
File: {1.5696852154551616956931498094999`31.999078724290577, 4.800000000000001}

if the amount of files in an array 4.800000000000001 law and not 4.8

thank you

POSTED BY: Paolo Pellegrini
8 Replies

OK thanks. to tell the truth, after scoring a software JavaV8, I was trying to make it in Mathematica. the puzzle is integermania Maybe I open a post for the entire project ...

POSTED BY: Paolo Pellegrini

Paolo,

A modified version of your floating point version is about 40% faster than your integer version:

SumLevf[k_] := 
 IntegerPart[Max[k]] + 
  N[Round[Total[FractionalPart[{2.6, 4.2}]], 10^-2]]

I rounded to the nearest 0.01 but you can change that to what you need. If you drop the N[] it is even 5-7% faster but your answer is a fraction so that may or may not be useful.

SumLevf[k_] := 
 IntegerPart[Max[k]] + Round[Total[FractionalPart[{2.6, 4.2}]], 10^-2]

Since I really do not understand why you take the two numbers and process them in this fashion I really can't help with any other shortcuts to the problem. (I focused only on the one line calculation).

I hope this helps

POSTED BY: Neil Singer

yeah sure, I need for a mathematical puzzle. calculation times are too long and have to be stored in multiple files all combinations of trigonometric functions grouped to 5, and then restart them for further calculations.

OperLevel1 = {arcCos, arcCosh, arcCot, arcCoth, arcCsc, arcCsch, arcSec, arcSech, arcSin, arcSinh, arcTan, arcTanh, cos, cosh, cot, coth, csc, csch, expx, log, sec, sech, sin, sinh, tan, tanh};
arcCos[{i_Real, k_Integer, j_String}] := {ArcCos[i], SumLev[{k, 42}], "ArcCos(" <> j <> ")"}; arcCos[_] = {0, 0, 0};
... ... ...
SumLev[k_] := Total[k] - Total[IntegerPart[k/10]*10] + IntegerPart[Max[k]/10]*10;
MatrixResult = Flatten[Table[OperLevel1[[$x1]][MatrixResult[[$x2]]], {$x1, 1, Length[OperLevel1]}, {$x2, 1, Length[MatrixResult]}], 1];

store the values in a file, it would save additional time calculation

POSTED BY: Paolo Pellegrini

Paolo,

Please describe what you are trying to do. We can be more helpful.

Regards

POSTED BY: Neil Singer

it will mean that I will use integers. how can I optimize and speed this up?

SumLev[k_] := Total[k] - Total[IntegerPart[k/10]*10] + IntegerPart[Max[k]/10]*10;
For[x = 1, x <= 10^6, x++, y = SumLev[26, 42]] // AbsoluteTiming
Out: {1.02184, Null}
POSTED BY: Paolo Pellegrini

ok, thank you, then you should SumLev the function. how can I make me return a number with one decimal place?

SumLev[k_] := N[IntegerPart[Max[k]] + Total[FractionalPart[k]], 2];
SumLev[{2.6, 4.2}] // FullForm
Out: 4.800000000000001`

the problem is here?

k = {2.6, 4.2}; Total[FractionalPart[k]] // FullForm
Out: 0.8000000000000003`
POSTED BY: Paolo Pellegrini

To complement Neil Singer's observation, the display rounding of machine-precision numbers is controlled by the front end option PrintPrecision, which by default is set to six digits. One can see the full precision of machine reals by setting the number of digits to sixteen:

SetOptions[$FrontEndSession, PrintPrecision-> 16]

Another consideration is why the result is not 4.8 exactly (which is what one would get by hand). All (or at least practically all) CPUs implement floating-point numbers in binary, and almost all currently use double-precision (binary64). It can represent exactly only numbers whose denominator is a power of 2 (in an appropriate finite range). There is some rounding error when a decimal fraction such as 2.6 or 4.2 is stored as a binary floating-point number. One might think that the fractional parts of 2.6 and 4.2 would be 0.6 and 0.2 and add to 0.8. But they do not, exactly:

FractionalPart[2.6]
FractionalPart[4.2]
% + %%
4 + %
(*
  0.6000000000000001
  0.2000000000000002
  0.8000000000000003
  4.800000000000001
*)
POSTED BY: Michael Rogers

Paolo,

the display is always rounded for visual appeal.

Do

$a // FullForm

and you see that your numbers are actually this (which is what goes into the file)

List[1.5696852154551616956931498094999`31.999078724290577,4.800000000000001`]

see Display of Numbers to get more details.

Regards

POSTED BY: Neil Singer
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