Jump to content

[HELP] ATOF Function rounds large numbers.


Robinbb

Recommended Posts

Hello CADians :D,

 

I couldn't find a solution to this problem anywhere online. I am also essentially a newbie when it comes to AutoLISP as I have only learned the language ad-hoc to meet certain needs of my business.

 

I have a lisp routine that reads a text file, each line has coordinates depths and descriptions. I use the substr function to get each item of info out of every line. Everything works fine up to this point.

 

However when I use ATOF (after some testing) it rounds the value to 6 significant figure. For example, if I use ATOF on the string "543334.243" it returns 543334.0, "9543334.243" returns 9.54333e+06 and "43334.243" returns 43334.2.

 

So when I use this routine to create points with more than 6 significant figures I get issues with the rounding. Is there an alternative function I can use? I have tried DISTOF but it gives me the same result. Is it a setting or something I can change?

 

I am currently using AutoCAD 2017 and I use the VLIDE to write and edit the code I use. This particular code was written a while ago originally for AutoCAD R14 it worked for that and still does actually.

 

Here is the Code

;READ_MRG.LSP
;reads ASCII M-file <name.ext> created by MERGE_WS.BAS
;and inserts PT blocks 950 on each layer of the type _PTS# 
;format of file must be:
;Name/1-7, East/9-19, North/21-31, Depth/33-41, Rmks/43-55,
;Seabed GPS El 57-65, Acc 67-68, Exact 70-71
;Exact shows whether interpolation was needed
;Read Hydro in ACAD File|ASC In/Out calls this program

;----------
(defun setcol()
  (setq na1 1 na2 7 ea1 9 ea2 11 no1 21 no2 11 dp1 33 dp2 9
        rm1 43 rm2 13 el1 57 el2 9 acc1 67 acc2 2 xa1 70 xa2 2)
)
;----------
(defun C:READ_MRG ( / fname s f tx name east north depth elev rmks pt1)
  (setvar "ATTDIA" 0)
  (setvar "ATTREQ" 1)
  ;get existing file
  (setq fname (getfiled "Select M... file: " "" "" 0))
  (if f (close f))
  (if (setq f (open fname "r"))
     (progn
;         (command "LAYER" "M" "_weak" "")
;         (command "LAYER" "M" "_bad" "")
        (princ "\nReading and inserting ...\n")
        (setcol)
        (setq tx (read-line f))
        (setq lyrnum 0)
        (setq ptnum 0)
        (while tx
           (if (= 0 ptnum)
              (progn
                 (setq lyrnum (+ 1 lyrnum))
                 (command "LAYER" "M" (strcat "_PTS" (rtos lyrnum 2 0)) "")
                 (setq ptnum 0)
              )
           )
           (if (/= (substr tx 1 1) ";")    ;ignore remark and header lines
              (progn
                 (putpt)
                 (if (= ptnum 949)
                    (setq ptnum 0)
                    (setq ptnum (+ 1 ptnum))
                 )
              )   
           )
           (setq tx (read-line f))
        )  ;while tx
        (close f)
     )
  )  ;if f
  (princ)
)
;----------
(defun putpt ()
  (setq 
     name (substr tx na1 na2)
     east (substr tx ea1 ea2)
     north (substr tx no1 no2)
     depth (substr tx dp1 dp2)
     rmks  (substr tx rm1 rm2)
     elev  (substr tx el1 el2)
     acc   (atoi (substr tx acc1 acc2))
     xact  (substr tx xa1 xa2)
     name (rtos (atof name) 2 0)
     [color="red"]east (atof east)
     north (atof north)
     depth (atof depth)[/color]
     pt1 (list east2 north2)
  )
  (if 
    (= depth2 -32767.0) 
      (setq depth2 "") 
      (setq depth2 (rtos depth 2 3))
  )
  (command "INSERT" "PT" pt1 1 1 0 depth2 name rmks)
;   (if 
;     (> acc 3) 
;       (command "CHPROP" "L" "" "LA" "_weak" "")
;   )
;   (if 
;     (> acc 6) 
;       (command "CHPROP" "L" "" "LA" "_bad" "")
;   )
)

 

Any help would be greatly appreciated. Thanks in Advance.

 

Robin

Link to comment
Share on other sites

You don't need any special function.

The value is shown truncated for convenience only.

Try to reverse the result back to string and you will see all the decimals.

(atof "9543334.243") -> 9.54333e+006
(rtos (atof "9543334.243") 2 3) -> "9543334.243"

Link to comment
Share on other sites

Thanks for the reply, the code I have highlighted red is where I try to store these atof values but when I run the routine the result is that the values are truncated so all my points are in the wrong position. I have used VLIDE 'watch window' to see what the value is that is passed on after using atof an it shows the truncated value in the window (shouldn't the watch window show me the actual value?).

During research I have read that AutoCAD internally keeps the value but shows a truncated value, however this routine doesn't display any text it uses the variables created from the text file to draw points and those points are created with truncated values and therefore all in a straight line so something isn't right with the code but I can't figure it out.

Link to comment
Share on other sites

AutoCAD stores coordinates as double precision real numbers. This means that numbers have approximately 15 to 17 significant figures (see https://en.wikipedia.org/wiki/Double-precision_floating-point_format for more information ).

 

For the following:

(setq x 123456789.123456789)

x will be displayed as: 1.23457e+008

 

Converting x to a string:

(setq xstring (rtos x))

yields: "123456789.1234568"

 

Converting the string to a real number yields a displayed value of:

(setq xreal (atof xstring))

1.23457e+008

which implies a roundoff at 6 significant figures.

 

But we can find that xreal has more precision by subtracting it from the original value of x.

(setq dif (- x xreal))

-1.49012e-008

 

This shows that x and xreal are the same to 16 significant figures (the 9 digits preceding the decimal point and 7 digits after the decimal point). xreal has more precision than its displayed value.

 

Be careful not to confuse displayed values with real values.

Link to comment
Share on other sites

Where do you define east2 and north2?

That was from debugging I changed the second reference of east and north to east2 and north2 to see if that would help. I changed it back just before I created the thread but I missed those two

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...